3

Well I learning Shell Script this one thing is annoying me I cant find what actually ${ } suppose to do

I google around to found out that ${ } is used for substitution

but what I not able to understand is here

If ${ } is suppose to do a substitution then

distro=("redhat" "debian" "gentoo")

echo ${distro[0]}
echo ${distro[2]}

echo ${#distro[@]} 

How come it running the above code when there isnt any substitution .

I'm wrong on this

Viren
  • 5,812
  • 6
  • 45
  • 98
  • http://tldp.org/LDP/abs/html/arrays.html – Mat May 29 '12 at 07:09
  • See my answer [here](http://stackoverflow.com/questions/2188199/bash-double-or-single-bracket-parentheses-curly-braces/2188369#2188369) which discusses curly braces, among other things. – Dennis Williamson May 29 '12 at 08:07
  • 1
    that is @DennisWilliamson(sorry cant vote it no reputation) I guess the ${ } is a substitution with @ as an exception as i see in the manual referred by -- KarolyHorvath – Viren May 29 '12 at 08:26

1 Answers1

4

man bash, search for ${ with /\${, press n a couple of time and voila..

Any element of an array may be referenced using ${name[subscript]}.

and

${#name[subscript]} expands to the length of ${name[subscript]}. If subscript is * or @, the expansion is the number of elements in the array.

Checking the previous paragraphs you will also find this:

Arrays are assigned to using compound assignments of the form name=(value1 ... valuen), where each value is of the form [subscript]=string. Indexed array assignments do not require the bracket and subscript.

Karoly Horvath
  • 94,607
  • 11
  • 117
  • 176