Alex Rivera | Logout

Bash ${!variable}

Asked 2012-04-25T13:38:02.540
11

I am scratching my head on this one, can't find the bash reference talking about it.

In the code below

host_color=${uphost}_host_color
host_color=${!host_color}

What is the second line doing ? what does the !operator do in this case ?

Edit
Report

1 Answer

2

From the bash manual:

If the first character of parameter is an exclamation point (!), a level of variable indirection is introduced. Bash uses the value of the variable formed from the rest of parameter as the name of the variable; this variable is then expanded and that value is used in the rest of the substitution, rather than the value of parameter itself. This is known as indirect expansion. The exceptions to this are the expansions of ${!prefix*} and ${!name[@]} described below.

answered 2012-04-25T13:45:49.067

Your Answer