Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Substitute a bash script variable twice

Tags:

bash

scripting

I would like to know if I can substitute a variable twice.

For example:

#global variable 
TEST_SERV_EXT=""

#variables become from myconf.sh
TEST_SERV_EXT_FO='foo01'
TEST_SERV_EXT_BR='bar01'

I want dynamically construct those last two and assign them in TEST_SERV_EXT.

I tried something like this ${$TEST_SERV_COMP} but I'm getting "bad substitution" message.

I need something like php's feature "$$" or tcl's subst command.

Regards, thandem

like image 533
thandem Avatar asked May 28 '13 11:05

thandem


People also ask

What is Bash variable substitution?

Bash performs variable substitution before the command is really executed. The Linux Bash Shell searches for all the ‘$’ sign before executing the command and replace it with the value of variable. The process of Bash Variable substitution is performed only once.

Why is there no double substitution in shell script?

It doesn't do a “double substitution” because there's no such feature in any of the common shells (not with this syntax anyway).

How do you use variables in Bash?

Bash variables and command substitution Using variables to refer to data, including the results of a command. An essential feature of programming is the ability to use a name or a label to refer to some other quantity: such as a value, or a command. This is commonly referred to as variables.

What is command substitution in Linux?

Command substitution The standard output of a command can be encapsulated, much like a value can be stored in a value, and then expanded by the shell. This is known as command substitution.


1 Answers

TEST_SERV_COMP=TEST_SERV_EXT_FO
TEST_SERV_EXT=${!TEST_SERV_COMP}

Look for indirect expansion in the bash manual.

like image 95
Uwe Avatar answered Oct 12 '22 11:10

Uwe