0

my code

TOTAL=${#FOO_5[*]} // COUNT ITEMS IN ARRAY

what if code

ARRAY_NAME="FOO_5"
TOTAL=${#${!ARRAY_NAME}[*]} //error

please fix

1
  • 1
    Don't guess randomly at shell syntax. Commented Oct 14, 2012 at 12:21

1 Answer 1

2

When it comes to indirect parameter expansion, the array index is considered part of the parameter name. Unfortunately, you cannot chain parameter expansions; you'll also need a intermediate variable.

ARRAY_NAME="FOO_5[*]"
FULL=${!ARRAY_NAME}
TOTAL=${#FULL}
Sign up to request clarification or add additional context in comments.

3 Comments

ugh. recommend associative arrays before this hack. Especially to "all-caps variable users".
Also forgot to mention, remember "${#a[*]}" and "${#a[@]}" are equivalent, so this is probably not what he wants. full=("${!arr}") total=${#full[@]}. I'd just eval.
Right, I missed the comment that said "count items in array".

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.