Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bash array from find command length

Tags:

arrays

shell

size

I have the following code:

filelist="$(find $name -type f | sort)";
echo "$filelist";
echo "${#filelist[@]}"

My array contains many elements but the last command says my array contains only one element. What am I doing wrong?

like image 731
fmonegaglia Avatar asked Nov 02 '12 16:11

fmonegaglia


People also ask

How do I find the length of an array in bash?

Getting the array length To get the length of an array, we can use the {#array[@]} syntax in bash. The # in the above syntax calculates the array size, without hash # it just returns all elements in the array.

How do I find the length of a variable in bash?

We can use the # operator to get the length of the string in BASH, we need to enclose the variable name enclosed in “{ }” and inside of that, we use the # to get the length of the string variable. Thus, using the “#” operator in BASH, we can get the length of the string variable.

How do I know my shell size?

`expr` command can be used by two ways to count the length of a string. Without `expr`, `wc` and `awk` command can also be used to count the length of a string.


1 Answers

You need to use parentheses to have bash recognize it as an array.

filelist=($(find $name -type f | sort))
echo ${#filelist[@]}
like image 76
evil otto Avatar answered Sep 16 '22 18:09

evil otto