LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 01-06-2005, 04:02 PM   #1
Duudson
Member
 
Registered: Dec 2004
Distribution: RHEL3, FC3
Posts: 53

Rep: Reputation: 15
bash, how to get variable name from variable


Hello, I have following script:

#!/bin/bash
A="abcd dcba"
B=A
C=$`echo $B`
echo $C

this script echos $A

How can I modify it so that it will echo the content of variable A?

So I want it to echo abcd dcba, any hints how to do it?
 
Old 01-06-2005, 04:12 PM   #2
iZvi
Member
 
Registered: Mar 2004
Location: Valhalla
Distribution: Slackware-current, kernel 2.6.31
Posts: 284

Rep: Reputation: 35
Look at this row:
B=A
It should make B equal to the contents of A, not to A itself.
Change it to B=$A
It should do the job.
 
Old 01-06-2005, 04:19 PM   #3
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 42
Don't know if this is the best way, but it works:
Code:
#!/bin/bash
A="abcd dcba"
B=A
C='eval "echo \$$B"'
eval "$C"
This way you can change B or A after setting C and the change should be reflected when you do the final eval.
 
Old 01-06-2005, 04:25 PM   #4
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
Bash has a special construction for this:
Code:
#!/bin/bash

A="abcd dcba"
B=A
C=${!B}
echo $C
 
Old 01-06-2005, 04:26 PM   #5
Duudson
Member
 
Registered: Dec 2004
Distribution: RHEL3, FC3
Posts: 53

Original Poster
Rep: Reputation: 15
Quote:
Originally posted by aluser
Don't know if this is the best way, but it works:
Code:
#!/bin/bash
A="abcd dcba"
B=A
C='eval "echo \$$B"'
eval "$C"
This way you can change B or A after setting C and the change should be reflected when you do the final eval.
Thank you, this seems to work too.

Last edited by Duudson; 01-06-2005 at 04:37 PM.
 
Old 01-06-2005, 04:36 PM   #6
Duudson
Member
 
Registered: Dec 2004
Distribution: RHEL3, FC3
Posts: 53

Original Poster
Rep: Reputation: 15
Quote:
Originally posted by Hko
Bash has a special construction for this:
Code:
#!/bin/bash

A="abcd dcba"
B=A
C=${!B}
echo $C
Ok, this is exactly what i was looking for!

THANK YOU VERY MUCH!

Last edited by Duudson; 01-06-2005 at 04:40 PM.
 
Old 01-06-2005, 04:38 PM   #7
Duudson
Member
 
Registered: Dec 2004
Distribution: RHEL3, FC3
Posts: 53

Original Poster
Rep: Reputation: 15
<double post>

Last edited by Duudson; 01-06-2005 at 04:40 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
bash month variable johniccp Linux - Newbie 1 12-06-2004 01:41 PM
Variable with bash pinkysioux Programming 6 04-15-2004 08:46 PM
bash - how to take input as a variable? trees Linux - General 1 02-19-2004 04:09 PM
Bash Variable Problem decates Linux - Newbie 10 01-28-2003 10:47 PM
BASH variable export Barbarian Programming 2 11-27-2001 08:37 PM


All times are GMT -5. The time now is 10:37 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration