LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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, 09:49 AM   #1
jnojr
Member
 
Registered: Sep 2007
Location: San Diego, CA
Posts: 202

Rep: Reputation: 18
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, 09:51 AM   #2
jnojr
Member
 
Registered: Sep 2007
Location: San Diego, CA
Posts: 202

Original Poster
Rep: Reputation: 18
N/M

eval newvar=\$$X

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

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
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, 03:48 PM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
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 03: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-05-2010 12:45 AM
unset export variable mira.mikes Linux - Desktop 4 03-17-2009 03:31 AM
export cd variable? rjwhite Linux - Games 1 12-17-2005 06:47 PM
Export variable loopoo Linux - Newbie 4 08-08-2005 05:08 AM


All times are GMT -5. The time now is 08: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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration