0

I have two lists

A='"foo" "bar"'
foo='"alpha" "beta"'
bar='"gamma" "delta"'

I want to iterate over list A and in a nested loop iterate over lists foo and bar so that i can execute a command in the nested loop where i can execute a commands like

./submit_to_cluster --x=foo --y=alpha

./submit_to_cluster --x=bar --y=delta

Is the iteration over the two lists in a nested manner the best way to do this efficiently?

1
  • 2
    What do beta and gamma do? Have you tried anything? Show us your code. Commented Jul 10, 2017 at 11:42

1 Answer 1

1

In your example A is not a list. If you need a list, use a list. This is a list: A=(foo bar). And this is how to reference an array: ${A[@]}.

What you are looking for is called indirect referencing. You have the name of a variable in a variable. This is indirect referencing: ${!v}.

You problem is: how to refer indirectly to an array?

This question has already been answered: How to iterate over an array using indirect reference?

This shows how to apply the answer to your example:

A=("foo" "bar")
foo=("alpha" "beta")
bar=("gamma" "delta")

for x in "${A[@]}"; do
  xa="${x}[@]"
  for y in "${!xa}"; do
    echo ./submit_to_cluster --x="$x" --y="$y"
  done
done

This prints:

./submit_to_cluster --x=foo --y=alpha
./submit_to_cluster --x=foo --y=beta
./submit_to_cluster --x=bar --y=gamma
./submit_to_cluster --x=bar --y=delta
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.