Question

Can someone explain the second result?

user$ set 5 5
user$ n=2
user$ eval echo \$$n
5
user$ echo `eval echo \$$n`
10268n

10268 is bash pid.

GNU bash, version 4.0.35(0)-release (i386-portbld-freebsd7.2)

UPD: This works fine:

user$ echo `eval echo \\$$n`
5

But then...

user$ echo `eval echo \\\$$n` #3
5
user$ echo `eval echo \\\\$$n` #4
10268n
user$ echo `eval echo \\\\\$$n` #5
10268n
user$ echo `eval echo \\\\\\$$n` #6
$2
user$ echo `eval echo \\\\\\\$$n` #7
$2
user$ echo `eval echo \\\\\\\\$$n` #8
$2
user$ echo `eval echo \\\\\\\\\$$n` #9
10268n
Was it helpful?

Solution

This line:

set 5 10

makes positional parameters $1=5 and makes $2=10

This line:

n=2

Sets shell variable n to value 2

Then this line:

eval echo \$$n

is effectively this:

echo $2

prints $2 which is 10

Finally this line:

echo `eval echo \$$n`

is same as (due to back ticks):

eval echo $$n

which is effectively this:

echo $$n

Prints $$ (current shell PID) and literal n hence prints

10268n
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top