[wplug] bash variable help

Jonathan S Billings billings at negate.org
Wed Feb 18 11:09:13 EST 2004


On Wed, 2004-02-18 at 10:31, 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}"
> }
> 
> 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


Try:
function() {
   eval "which_var=var${1}"
}

(shells aren't as smart about interpolation as languates such as perl or
php, so you have to force it to basically run another subshell)
-- 
Jonathan S Billings <billings at negate.org>




More information about the wplug mailing list