3.2. Quotes and Special Characters

If you wish to include a special character in a variable, you will have to quote it differently:

> newvar=$testvar
> echo $newvar
5
> newvar="$testvar"
> echo $newvar
5
> newvar='$testvar'
> echo $newvar
$testvar
> newvar=\$testvar
> echo $newvar
$testvar
>

The dollar sign isn't the only character that's special to the Bash shell, but it's a simple example. An interesting step we can take to make use of assigning a variable name to another variable name is to use eval to dereference the stored variable name:

> echo $testvar
5
> echo $newvar
$testvar
> eval echo $newvar
5
> 

Normally, the shell does only one round of substitutions on the expression it is evaluating: if you say echo $newvar the shell will only go so far as to determine that $newvar is equal to the text string $testvar, it won't evaluate what $testvar is equal to. eval forces that evaluation.

mirror server hosted at Truenetwork, Russian Federation.