I have two variables in bash that complete the name of another one, and I want to expand it but don’t know how to do it
I have:
echo $sala
a
echo $i
10
and I want to expand ${a10} in this form ${$sala$i} but apparently the {} scape the $ signs.
There are a few ways, with different advantages and disadvantages. The safest way is to save the complete parameter name in a single parameter, and then use indirection to expand it:
A slightly simpler way is a command like this:
which will run
evalwith the argumentsechoand${a10}, and therefore runecho ${a10}. This way is less safe in general — its behavior depends a bit more chaotically on the values of the parameters — but it doesn’t require a temporary variable.