209

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

For example:

arr=(1 2 3 4 5)

And I want to get its length, which is 5 in this case.

0

7 Answers 7

282
$ a=(1 2 3 4)
$ echo ${#a[@]}
4
7
  • 14
    What does @ do here? May 29, 2017 at 7:50
  • 22
    @AhmedAkhtar There's a decent explanation here. Basically, [*] and [@] both "explode" arrays into a tokenized string, but [@] can preserve spaces within tokens. However, when counting elements, it doesn't appear to matter; arr=(foo "bar baz"); echo ${arr[*]} prints 2, not 3. May 23, 2018 at 19:56
  • 1
    This worked for me but only after removing [@] on Mac. GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin19) if anyone's having the same issue.
    – Prav
    Jul 21, 2020 at 16:54
  • 1
    Without the [@] it returns the length of the first element for me (using Bash 4.4.20(1)-release).
    – Sussch
    Nov 3, 2020 at 8:44
  • 5
    @PraveenPremaratne I tested ${#a[@]} in bash 2.00.0(2) (the oldest version I could compile. This is from 1996!) and bash 3.2.57(1) (the version used on macOS). The command worked as expected in both version. I guess you must have made an error somewhere else.
    – Socowi
    Aug 17, 2021 at 11:19
48

From Bash manual:

${#parameter}

The length in characters of the expanded value of parameter is substituted. If parameter is ‘*’ or ‘@’, the value substituted is the number of positional parameters. If parameter is an array name subscripted by ‘*’ or ‘@’, the value substituted is the number of elements in the array. If parameter is an indexed array name subscripted by a negative number, that number is interpreted as relative to one greater than the maximum index of parameter, so negative indices count back from the end of the array, and an index of -1 references the last element.

Length of strings, arrays, and associative arrays

string="0123456789"                   # create a string of 10 characters
array=(0 1 2 3 4 5 6 7 8 9)           # create an indexed array of 10 elements
declare -A hash
hash=([one]=1 [two]=2 [three]=3)      # create an associative array of 3 elements
echo "string length is: ${#string}"   # length of string
echo "array length is: ${#array[@]}"  # length of array using @ as the index
echo "array length is: ${#array[*]}"  # length of array using * as the index
echo "hash length is: ${#hash[@]}"    # length of array using @ as the index
echo "hash length is: ${#hash[*]}"    # length of array using * as the index

output:

string length is: 10
array length is: 10
array length is: 10
hash length is: 3
hash length is: 3

Dealing with $@, the argument array:

set arg1 arg2 "arg 3"
args_copy=("$@")
echo "number of args is: $#"
echo "number of args is: ${#@}"
echo "args_copy length is: ${#args_copy[@]}"

output:

number of args is: 3
number of args is: 3
args_copy length is: 3
0
28

Assuming bash:

~> declare -a foo
~> foo[0]="foo"
~> foo[1]="bar"
~> foo[2]="baz"
~> echo ${#foo[*]}
3

So, ${#ARRAY[*]} expands to the length of the array ARRAY.

3
  • This question is pretty old but I would like to know how to store this length of array in one variable? I tried something like foo=${#foo[*]} but shell is throwing command not found error.
    – Shekhar
    Mar 19, 2013 at 9:55
  • 4
    What is *? How does it differ from @? Dec 5, 2017 at 9:37
  • @jameshfisher It doesn't, in this usage.
    – unwind
    Dec 5, 2017 at 15:01
9

in tcsh or csh:

~> set a = ( 1 2 3 4 5 )
~> echo $#a
5
1
  • None of the above working for me ! This works perfect!! Could you please more detail?? May 22, 2016 at 10:44
6

In the Fish Shell the length of an array can be found with:

$ set a 1 2 3 4
$ count $a
4
2
  • I don't believe there is a count command in Unix. Which OS are you using? Mar 26, 2018 at 19:14
  • 4
    @codeforester It's a shell command, obviously available in the Fish shell. OS doesn't really matter.
    – matli
    Mar 26, 2018 at 22:03
5

This works well for me:

arglen=$#
argparam=$*
if [ $arglen -eq '3' ];
then
    echo Valid Number of arguments
    echo "Arguments are $*"
else
    echo only four arguments are allowed
fi
-4

For those who still searching a way to put the length of an array into a variable:

foo="${#ARRAY[*]}"
2
  • This is the right, short answer
    – Brethlosze
    Mar 31, 2023 at 22:28
  • Ironically, this is almost identical to the most upvoted answer.
    – Shōgun8
    Nov 7, 2023 at 17:54

Not the answer you're looking for? Browse other questions tagged or ask your own question.