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?

Satyam
  • 645
  • 2
  • 7
  • 20

1 Answers1

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
ceving
  • 21,900
  • 13
  • 104
  • 178