Computing.Net > Forums > Unix > Variable Variables

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Variable Variables

Reply to Message Icon

Name: jakekatz
Date: December 4, 2007 at 09:32:58 Pacific
OS: Solaris 10 Update #4
CPU/Ram: Sparc
Product: Sun SunBlade 1500
Comment:

How to create a variable partially from another variable. Can it be done?

I'm trying to do this;

for X in A B; do
Site_${X}="Value of $X"
done

bash: Site_A=Value of A: command not found
bash: Site_B=Value of B: command not found

I would like the variables Site_A & Site_B to contain some values.

Any ideas?



Sponsored Link
Ads by Google

Response Number 1
Name: James Boothe
Date: December 4, 2007 at 10:05:42 Pacific
Reply:

Since your variable name includes a variable, the shell needs to parse (evaluate) the variable assignment line twice.  And to prevent the shell from removing the double-quotes on the initial evaluation (which would cause a syntax error), I protect them with back-slashes.

for X in A B; do
eval Site_${X}=\"Value of $X\"
done

echo $Site_A
Value of A
echo $Site_B
Value of B

So the line to be processed by the shell is:
eval Site_${X}=\"Value of $X\"

After the initial processing by the shell, we now have:
eval Site_A="Value of A"

Then the eval command causes the result to be processed again:
Site_A="Value of A"


0

Response Number 2
Name: jakekatz
Date: December 4, 2007 at 11:13:55 Pacific
Reply:

That's it!

I must learn the value of the 'eval' command :)

Thank you ever so much.

Jake.


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More


basic function of awk need help for sed



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: Variable Variables

Variables in Shell SCripting www.computing.net/answers/unix/variables-in-shell-scripting/6020.html

export variables www.computing.net/answers/unix/export-variables/4268.html

variables www.computing.net/answers/unix/variables/3926.html