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[@]}
)?
|
|||
|
In zsh, personal preference. In other shells So, if you want to be little more portable between shells specifying the In zsh,
Changing I prefer |
|||||
|
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 |
|||||
|
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. |
||||
|