0

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 Answers 2

2

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

eval echo \${#$1[@]}
Sign up to request clarification or add additional context in comments.

Comments

0
eval echo '${#'$1'[@]};'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.