LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-08-2012, 08:49 AM   #1
jnojr
Member
 
Registered: Sep 2007
Location: Chandler, AZ
Posts: 235

Rep: Reputation: 20
Export variable as contents of another variable


I'm sure this is ridiculously easy, but my brain is seizing on it

If we have:

A=1
B=2
C=3

X=`some command that will return A, B, or C`

How do I get a variable that will look at $X, see A B or C, and return the value of $A, $B, or $C?
 
Old 05-08-2012, 08:51 AM   #2
jnojr
Member
 
Registered: Sep 2007
Location: Chandler, AZ
Posts: 235

Original Poster
Rep: Reputation: 20
N/M

eval newvar=\$$X

Last edited by jnojr; 05-08-2012 at 09:08 AM.
 
Old 05-08-2012, 10:15 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,035

Rep: Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203
Please do not use eval if you can help it (search the net for many reasons why not):
Code:
A=1

X=A

echo ${!X}
 
Old 05-08-2012, 02:48 PM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Indirect variable reference is so awkward and untransparent. You're also limited to only using strings that match proper variable syntax; only letters, digits, and the underscore are allowed, and it can't begin with a digit.

Assuming bash or another shell that supports them, an associative array would be better. Then you can use any arbitrary string:

Code:
#!/bin/bash

declare -A array

array=( [A]=1 [B]=2 [C]=3 )


for x in A B C ; do

	echo "The value for entry $x is: ${array[$x]}"

done

How can I use variable variables (indirect variables, pointers, references) or associative arrays?
http://mywiki.wooledge.org/BashFAQ/006


BTW, environment variables are generally all upper-case. So while not critical, it's good practice to keep your own user variables in lower-case or mixed-case, to help differentiate them.

Last edited by David the H.; 05-08-2012 at 02:55 PM. Reason: elaboration
 
  


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
export a variable vinaytp Linux - Newbie 2 02-04-2010 11:45 PM
unset export variable mira.mikes Linux - Desktop 4 03-17-2009 02:31 AM
export cd variable? rjwhite Linux - Games 1 12-17-2005 05:47 PM
Export variable loopoo Linux - Newbie 4 08-08-2005 04:08 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:45 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration