0

I want to expand a parameter twice, and ${$PARAM} doesn't work.

For example, if I set two variables to file names and then want to loop through those variables and expand to their file names:

INPF_1=input1.inp
INPF_2=input2.inp

# copy the input files to the execution directory
for input_param in ${!INPF_*}; do
    echo ${$input_param}
done

How can I to access the file names from those parameters in the for loop?

I.e., expand to input1.inp and input2.inp.

Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
salotz
  • 429
  • 4
  • 20

1 Answers1

2

You had it almost: just use "${!input_param}" instead of ${$input_param}.

The quoting doesn't do anything in this case, but it's a good habit.

Be aware of the difference to ${!INPF_@}, which – when used between double quotes – expands to a separate word for each variable name, whereas "${!INPF_*}" doesn't. You almost always want "${!INPF_@}". See the difference:

$ for var in  "${!INPF_*}"; do echo "$var"; done
INPF_1 INPF_2
$ for var in  "${!INPF_@}"; do echo "$var"; done
INPF_1
INPF_2

See the manual for the various parameter expansions.

Benjamin W.
  • 46,058
  • 19
  • 106
  • 116