Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

reassigning a variable

 
yuga devi
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have the following code:
a1="welcome"
a2=a1
echo $a1 --> prints welcome
echo $a2 -> prints a1

echo $$a2 <---- In this line i am expecting to print 'welcome'...
But i am getting the output as "ksh: 21271a2: not found ".

Please help.


Thanks,
Yugandra
 
Joe Ess
Bartender
Pie
Posts: 9258
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My wild guess:

 
yuga devi
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am expecting something like echo $$a2 - one statement which should execute and print the value of a1.

thanks,
---yugandra
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use "eval". There might be a better way, but something like

eval "echo $"$a2

will print "welcome".
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 11854
185
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
$$ is a special parameter that most shells interpret as the process ID of the shell. You should be able to see this by typing echo $$ on the command line and noting the number. Then try ps - one of the shells listed (perhaps the only shell, depending on your OS) should have the same process ID.

I am a little surprised at you getting a "not found" message on an echo statement - it should have just echoed 21271a2 (the process ID with "a2" concatenated).
 
yuga devi
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,



eval "echo $"$a2


It works

Thankyou,
Yugandra
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic