Go Back   The UNIX and Linux Forums > Operating Systems > Linux
google site



Linux RedHat, Ubuntu, SUSE, Fedora, Debian, Mandriva, Slackware, Gentoo linux, PCLinuxOS. All Linux questions here!

Reply
English Japanese Spanish French German Portuguese Italian Powered by Powered by Google
 
Thread Tools Search this Thread Display Modes
  #1  
Old 03-12-2010
Registered User
 

Join Date: Nov 2009
Posts: 16
Thanks: 2
Thanked 0 Times in 0 Posts
How to get an Indirect Variable Value..?

Hi,

I've got a small problem.

If varible A stores "B" and Variable B stores C,
How to get the value of variable B by using only Variable A..?

I tried the following but didnt work pease help..


Code:
 
$ var1=vikram
$ echo $var1
vikram
$ vikram=sampath
$ echo $vikram
sampath
$ echo $`echo $var1`
$vikram

In the above code
Why i'm getting "$vikram" when i was expecting "sampath" (because $vikram=sampath)

Please help me the right way to decode in the above situation..

thanks

Vikram shetty
Sponsored Links
  #2  
Old 03-12-2010
ungalnanban's Avatar
Registered User
 

Join Date: Feb 2010
Location: Chennai
Posts: 112
Thanks: 1
Thanked 4 Times in 3 Posts
Thumbs up

See the following code.


Code:
 var1=vikram
 echo $var1
vikram=sampath
 echo $vikram
 eval echo \$$var1

[or]

Code:
 eval echo \$$(echo $var1)

[or]


Code:
   eval echo \$`echo $var1`

  #3  
Old 03-12-2010
thillai_selvan's Avatar
Registered User
 

Join Date: Feb 2010
Location: Chennai
Posts: 182
Thanks: 0
Thanked 0 Times in 0 Posts

Code:
eval result=\$$var1
echo $result

It will print the word sampath
  #4  
Old 03-12-2010
Registered User
 

Join Date: Nov 2009
Posts: 16
Thanks: 2
Thanked 0 Times in 0 Posts
thanks u very much ungalnanbu and selvan..

Its working fine..

Could you please pass on me any material that describes the above behaviour of variables..

thanks

Vikram shetty
  #5  
Old 03-12-2010
thillai_selvan's Avatar
Registered User
 

Join Date: Feb 2010
Location: Chennai
Posts: 182
Thanks: 0
Thanked 0 Times in 0 Posts
Indirect References.
http://www.linuxjournal.com/article/7385
Please go through the above URL.
This will help you to know about variables.

Last edited by thillai_selvan; 03-12-2010 at 01:31 AM..
  #6  
Old 03-12-2010
ungalnanban's Avatar
Registered User
 

Join Date: Feb 2010
Location: Chennai
Posts: 112
Thanks: 1
Thanked 4 Times in 3 Posts
Thumbs up

Refer the following links.

Advanced Bash-Scripting Guide
Very Simple Bash Scripts
Linux Shell Scripting Tutorial - A Beginner's handbook
  #7  
Old 03-12-2010
Registered User
 

Join Date: Nov 2009
Posts: 16
Thanks: 2
Thanked 0 Times in 0 Posts
Thank you very much guys..

warm regards
Vikram Shetty
Sponsored Links
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to define a variable with variable definition is stored in a variable? freddy1228 Shell Programming and Scripting 1 11-12-2009 03:39 AM
Insert a line including Variable & Carriage Return / sed command as Variable lowmaster Shell Programming and Scripting 2 05-19-2009 11:26 PM
Length of an indirect variable gone_bush Shell Programming and Scripting 0 03-28-2006 07:17 PM
Compound indirect variable references tkrussel UNIX for Advanced & Expert Users 5 08-21-2005 10:03 AM



All times are GMT -4. The time now is 08:02 AM.