Wednesday, August 26, 2009

bash {2..4} Variable Indirection

Bash v4 uses new quoting syntax, $"...", to do locale-specific string translation. Some scripts of mine that used bash-1.14 style variable indrection need to change. I was doing something like this to get the value of a variable whose name is the value of a second variable:
eval var2=$"$var1"
Turns out a variable indirection feature has been around since bash 2! Sheesh!
var2=${!var1}
is the official way to do this. Alternately this should also do:
eval var2=\$${var1}