This works:
echo ${!var_pointing_to_another_var}
This doesn't:
echo ${!${var}_string_suffix}
In other words, i have i have a var consisting of two parts: first is another variable, second is string suffix. Together, they form a var that would point to another var. But I get a bad substitution error.
Due to readability and security issues, I want to avoid eval command.
It's a syntax error that occurs when you execute your Bash script and it can be caused by different reasons. Two common causes for this error are the incorrect use of command substitution and incorrect characters added to the lines of your script (for example extra dollar signs or white spaces).
It's a typographical fault that happens when you run your Shell script, and it can happen for a variety of reasons. The wrong use of instruction substitution and erroneous characters appended to the program are two major reasons for this.
Parameter expansion. Here the braces {} are not being used as apart of a sequence builder, but as a way of generating parameter expansion. Parameter expansion involves what it says on the box: it takes the variable or expression within the braces and expands it to whatever it represents.
The curly braces tell the shell interpreter where the end of the variable name is.
as far as I know this is the only way.
t="${var}_string_suffix"
echo "${!t}"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With