1

suppose I have 2 arrays:

A=(a1 a2 a3)
B=(b1 b2 b3)

I want to display contents of A and B with a loop, something like this:

for i in A B
do
    echo ${${i}[@]}
done

and get results like this

a1 a2 a3
b1 b2 b3

how can I realize this?

1 Answer 1

2

Use indirect parameter expansion.

for i in A B; do
  t="$i[@]"   # A[@] or B[@]
  echo "${!t}"
done

The trick is to treat the array name plus the index ([@]) together as a variable "name".

Sign up to request clarification or add additional context in comments.

Comments

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.