How to find the length of an array in shell?

ArraysBashShell

Arrays Problem Overview


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.

Arrays Solutions


Solution 1 - Arrays

$ a=(1 2 3 4)
$ echo ${#a[@]}
4

Solution 2 - Arrays

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

Solution 3 - Arrays

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.

Solution 4 - Arrays

in tcsh or csh:

~> set a = ( 1 2 3 4 5 )
~> echo $#a
5

Solution 5 - Arrays

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

$ set a 1 2 3 4
$ count $a
4

Solution 6 - Arrays

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

Solution 7 - Arrays

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

foo=$(echo ${'ARRAY[*]}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionArunachalamView Question on Stackoverflow
Solution 1 - Arraysghostdog74View Answer on Stackoverflow
Solution 2 - ArrayscodeforesterView Answer on Stackoverflow
Solution 3 - ArraysunwindView Answer on Stackoverflow
Solution 4 - ArraysRahulView Answer on Stackoverflow
Solution 5 - ArraysharmView Answer on Stackoverflow
Solution 6 - ArraysMansur Ul HasanView Answer on Stackoverflow
Solution 7 - Arraysuser3771114View Answer on Stackoverflow