[wplug] bash variable help

Rick Smith rick at rbsmith.com
Wed Feb 18 12:08:23 EST 2004


On Wed, Feb 18, 2004 at 10:31:13AM -0500, Henry Umansky wrote:
> I'm having a lot of trouble writing a bash script.  Here's what I want to 
> do:
> 
> I have a file that contains multple variables
> 
> #file1
> var1="value1"
> var2="value2"
> var3="value3"
> 
> Then I have another file that sources file1 and has a function that takes 
> one argument.  The argument can be either a 1, 2, or 3.  That function, 
> depending on the argument, will use var1, var2, var3, respectively.
> 
> #file 2
> function() {
>    which_var = "var${1}"
> }

Try:
eval which_var=\$var$1

eval does a two pass.  The \$ will translate to $ on the first pass,
and expand the way you want on the second.


> 
> Now,  the variable $which_var either has a value of "var1", "var2", or 
> "var3".  How would I use $which_var to extract the value of var1, var2, 
> etc, so I get the value "value1", "value2", or "value3" instead of the 
> value "var1", "var2", or "var3".  Unfortunately, I can't pass a var1, a 
> var2, or a var3 to the function instead of 1, 2, 3, because the real script 
> I'm writing will not know the variable name ahead of time.  Any help would 
> be much appreciated.
> 
> -Henry
> _______________________________________________
> wplug mailing list
> wplug at wplug.org
> http://www.wplug.org/mailman/listinfo/wplug
> 



More information about the wplug mailing list