Array size in unix

June 13, 2011 at 04:50:13
Specs: unix
I have declared array in unix

a="a b c d e"

How to get the size of this array?

I tried ${#a[*]} but doesnot work kindly help...


See More: Array size in unix

Report •

#1
June 13, 2011 at 06:27:03
First, which shell you are using? I am assuming the bash shell since this is the Linux forum. You have defined an array of one element so the length of the array:

${#a[*]}

equals one.

I am betting that you think that array a contains elements a, b, c, d, e and the length should be 5:

#!/bin/bash

# syntax to declare an array of 5 elements in bash
declare -a a=(a b c d e)

echo ${#a[*]} # length should be 5


Check out this link for how to handle bash arrays:

http://www.thegeekstuff.com/2010/06...


Report •
Related Solutions


Ask Question