pattern1=book
{
x=1
eval echo \$pattern$x
}
book (this is the output)
But when I assign a variable to the output of the eval it doesn't work unless I prefix 2 times backslash before $ as shown below.
{
a=`eval echo \\$pattern$x`
echo $a
}
book
Why here twice "\" has to be prefixed.
The first time is when bash sees the \ it needs to be escaped: \\. The next requirement is for eval, where bash needs to see \$ so the value "$pattern" is actually seen by the shell that eval runs.
It's not necessary to double the backslash when using the modern command substitution syntax, $( ... )
, instead of the old, deprecated, and more complicated backtick syntax, ` ... `
.
Regards,
Alister
eval is not necessary here, fortunately.
read $pattern$x <<EOF
variablecontents
EOF