tags:

views:

59

answers:

2

The script I'm writing will require me to pass some command line parameters. I would like to use these parameters within an array, but I'm not sure how.

A very basic example of this would be (script run as ./script.sh array1):

#!/bin/bash
array1=( a b c d )

echo ${#$1[@]}

The output should be 4, but I receive the following error: line 5: ${#$1[@]}: bad substitution.

I don't have to use arrays, but would like to.

Thanks for any ideas

+2  A: 

you need to get bash to substitute the value of $1 before evaluating the line, try this...

eval echo \${#$1[@]}
Paul Creasey
Worked perfectly, thanks!
Chappy
A: 
eval echo '${#'$1'[@]};'
Michael Krelin - hacker