×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

bash script bad substitution error message

bash script bad substitution error message

bash script bad substitution error message

(OP)
Each item in the list is an associated array. I'm trying to traverse each key/value from each associated array. Error message I get ... ${!$x[@]}: bad substitution

I tried substituting with backticks and quotations ..

aaList=( aa1 aa2 aa3 )
for x in "${aaList[@]}"
do
for KEY in "${!$x[@]}"; do
echo "$KEY" "${$x[$KEY]}"
done
echo
done

-------------

RE: bash script bad substitution error message

Well, I'm new to tek-tips (I've just registered) and I see your question has still no answer. I'm pretty sure you have the answer since, but anyway, here is mine:
parameter expansion does not accept nesting (you can't do ${${var}}. You can have an indirection with '!' ( ${!var} ), but in the case of associative arrays, the indirection is used to access the keys ( "${!AA[@]}" ). Anyway, what follows the opening '{' (or the '!' - or '#' following it) is necessarily a NAME and not another parameter expansion. So you have to transform the parameter expansion of x first to the final name of the array variable.
Example :
$ unset a b ; declare -A a b ; for i in k{1..6} ; do a[a$i]=a_val_$i b[b$i]=b_val_$i ; done ; echo -e "${a[@]}\n${b[@]}"
a_val_k1 a_val_k2 a_val_k3 a_val_k4 a_val_k5 a_val_k6
b_val_k6 b_val_k5 b_val_k4 b_val_k3 b_val_k2 b_val_k1
declare -a c
c=( a b )
for x in "${c[@]}" ; do for k in $( eval echo "\${!$x[@]}" ) ; do echo -n "$k = " ; eval echo "\${$x[$k]}" ; done ; done
ak1 = a_val_k1
ak2 = a_val_k2
ak3 = a_val_k3
ak4 = a_val_k4
ak5 = a_val_k5
ak6 = a_val_k6
bk6 = b_val_k6
bk5 = b_val_k5
bk4 = b_val_k4
bk3 = b_val_k3
bk2 = b_val_k2
bk1 = b_val_k1

Not so straightforward as you thought ... but it works :)

RE: bash script bad substitution error message

revised and simplified version using the new -n declaration :
unset a b c x
declare -A a b ; for i in k{1..2} ; do a[a$i]=a_val_$i b[b$i]=b_val_$i ; done ; echo -e "${a[@]}\n${b[@]}"
declare -n x
declare -a c=( a b )
for x in "${c[@]}" ; do for k in "${!x[@]}" ; do echo -n "$k = " ; echo "${x[$k]}" ; done ; done

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close