How do I count the number of elements in an array using a bash shell script?



  • Usage is ${#name[*]}

    or ${#name[@]}

    Define an array and count the number of elements in the array.

    declare -A MyBucketList
    MyBucketList=([1]="See The Grand Canyon" [2]="Go to Mars" [3]="Climb Mount Everest")
    echo ${#MyBucketList[*]}
    

    Running this script give the following results.

    $ ./associative_array.sh
    3
    

Log in to reply
 

© Lightnetics 2024