Skip to content
Advertisement

How can I look up a variable by name with #!/bin/sh (POSIX sh)?

f1="filename1";
i=1;
c=f$i
echo $c

What shell command should I use so that echo $c returns “filename1” as the output?

Advertisement

Answer

You can use eval to “nest” variable substitutions.

f1="filename1";
i=1;
eval c=${f$i}
echo $c
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement