Monday, December 5, 2011

Split sentences in arrays

ubuntu1004@ubuntu1004:~$ read str
my first string      
ubuntu1004@ubuntu1004:~$ echo $str
my first string
ubuntu1004@ubuntu1004:~$ read -a str
This is an array
ubuntu1004@ubuntu1004:~$ echo ${str[0]}
This
ubuntu1004@ubuntu1004:~$ echo ${str[2]}
an
ubuntu1004@ubuntu1004:~$ echo ${str[@]}
This is an array
ubuntu1004@ubuntu1004:~$ echo ${#str[@]}
4
ubuntu1004@ubuntu1004:~$ read -a str <<<'This is another string'
ubuntu1004@ubuntu1004:~$ echo ${#str[@]}
4
ubuntu1004@ubuntu1004:~$ echo ${str[@]}
This is another string
ubuntu1004@ubuntu1004:~$ echo ${str[2]}
another

No comments:

Post a Comment