aspose file tools*
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
JavaRanch » Java Forums » Engineering » Linux / UNIX
Bookmark "reassigning a variable" Watch "reassigning a variable" New topic
Author

reassigning a variable

yuga devi
Greenhorn

Joined: Aug 07, 2008
Posts: 22
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

Joined: Oct 29, 2001
Posts: 8908
    
    8

My wild guess:



"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
yuga devi
Greenhorn

Joined: Aug 07, 2008
Posts: 22
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

Joined: Jul 08, 2003
Posts: 24184
    
  34

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: 11460
    
  94

$$ 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
Greenhorn

Joined: Aug 07, 2008
Posts: 22
Hi,



eval "echo $"$a2


It works

Thankyou,
Yugandra
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: reassigning a variable