This week's book giveaway is in the JavaFX forum.
We're giving away four copies of Introducing JavaFX 8 Programming and have Herbert Schildt on-line!
See this thread for details.
The moose likes Linux / UNIX and the fly likes reassigning a variable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Introducing JavaFX 8 Programming this week in the JavaFX forum!
JavaRanch » Java Forums » Engineering » Linux / UNIX
Bookmark "reassigning a variable" Watch "reassigning a variable" New topic

reassigning a variable

yuga devi

Joined: Aug 07, 2008
Posts: 22

I have the following code:
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.

Joe Ess

Joined: Oct 29, 2001
Posts: 9150

My wild guess:

[How To Ask Questions On JavaRanch]
yuga devi

Joined: Aug 07, 2008
Posts: 22

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

Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24193

Use "eval". There might be a better way, but something like

eval "echo $"$a2

will print "welcome".

[Jess in Action][AskingGoodQuestions]
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11745

$$ 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).

The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
yuga devi

Joined: Aug 07, 2008
Posts: 22

eval "echo $"$a2

It works

I agree. Here's the link:
subject: reassigning a variable
It's not a secret anymore!