Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Double curly braces in bash - bad substitution

Tags:

bash

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.

like image 846
flying_potato_chip Avatar asked Dec 29 '14 03:12

flying_potato_chip


People also ask

What does bad substitution mean in Bash?

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).

What is bad substitution in shell script?

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.

What are {} used for in Bash?

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.

What does curly brackets mean in Bash?

The curly braces tell the shell interpreter where the end of the variable name is.


1 Answers

as far as I know this is the only way.

t="${var}_string_suffix"
echo "${!t}"
like image 172
Jasen Avatar answered Oct 03 '22 00:10

Jasen