You need to store the output in an array to find the length of the array:
$ b=($(seq 1 1 10))
$ echo ${#b[@]}
10
Saying b=$(seq 1 1 10)
doesn't produce an array.
Вопрос
I am new to shell scripting and I am trying a simple task of getting the length of a sequence of numbers generated using seq
.
With the help of a related post here: How to find the array length in unix shell? I was able to do this -
a=(1 2 3 4 5)
echo ${#a[@]} #length of a
5 #length of a = 5 (This is fine !!)
However when I try to do a similar thing using seq
..
b=$(seq 1 1 10)
echo $b
1 2 3 4 5 6 7 8 9 10
echo ${#b[@]}
1 #the length of b is 1, while I expect it to be 10
Why does this happen ? Are the variable types a and b different? is b not an array ?
I am sure I am missing something very trivial here, help is greatly appreciated.
Thanks
Ashwin
Решение
You need to store the output in an array to find the length of the array:
$ b=($(seq 1 1 10))
$ echo ${#b[@]}
10
Saying b=$(seq 1 1 10)
doesn't produce an array.
Другие советы
Try
echo ${b[0]}
It will be 1 2 3 4 5 6 7 8 9 10 because all your values are stored in first element of array a as a string.
b=($(seq 1 1 10))
will do what you want.