1

In the UNIX terminal, when I write the following commands:

$ a=b

$ c=a

$ echo $$c

I expected the output to be b, since the value of c is a and the value of a is b.

But , instead the output I received was: 2861c.

Can someone tell me the reason behind this output?

Ayush Shridhar
  • 115
  • 4
  • 11

3 Answers3

1

echo $$c prints your terminal PID and letter 'c' after it. You can verify it by 'ps aux | grep bash'.

HuTa
  • 168
  • 1
  • 8
0

$$ is special variable for BASH and used to print the process id of execution of current script. So $$c prints process id followed by c letter

If you still want to archive Indirect reference to variable,

a=b                                                                             
c=a                                                                             
echo ${!c} #will print "b" on console
toxic_boi_8041
  • 1,424
  • 16
  • 26
0

From http://www.tldp.org/LDP/abs/html/internalvariables.html

$$ gives PID of the current running shell instance.

bash4$ echo $$ 11015

bash4$ echo $BASHPID 11015

The first $ sign captures the next character and prints the value.

For your case, a double substitution is the best option.

echo ${!c}

or you may go for

eval echo \$$c

Palash Goel
  • 624
  • 6
  • 17