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
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.
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
Summary: Shell Scripting -- Variables In the main script i have exported the variable and accessing it in another file but when i echo the value the value of the exported variable is not viewable . below are ...
Summary: I am having trouble exporting variables set in a script to a function inside the script. 1. The variable must be set outside the function because it will be called outside as well as inside the functi...
Summary: Hi I have a string in one variable str1="'abcd '" and i want to replace spaces with * and result in another variable str2. Pl. help me how to do with shell script. str1="'abcd '" str2 should b...