As far as I can tell both ${#array[@]}
and ${#array}
evaluate to the number of elements in $array
. Is there any reason for preferring the longer form (${#array[@]}
)?
Tell me more
×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.
|
|||
|
In most shells that support arrays (I've tested it in bash, ksh93 and pdksh), ${#array} will give the length of ${array[0]}, and ${#array[@]} will give the number of elements in the array. This is logical: "${array}" returns element 0 of the array and "${#array}" returns its length. | ||||
|
As with anything, the more readable and understandable your code the easier it is for others (or future you) to maintain. When you have a choice and one of them is ambiguous, choose the other. Additionally, if you want portability in your shell scripts, the latter is the only one that works properly in | |||||
|