help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-bash] taking the name of a variable from another variable


From: Daniel Mills
Subject: Re: [Help-bash] taking the name of a variable from another variable
Date: Tue, 3 Jul 2018 09:57:20 -0400

On Tue, Jul 3, 2018 at 9:56 AM João Eiras <address@hidden> wrote:

> On 3 July 2018 at 14:09, Robert Durkacz <address@hidden> wrote:
> > How could I write a shell script that takes two arguments, the first to
> be
> > interpreted as the name of an environment variable and the second to be
> the
> > value of that variable? All the script should do is create an environment
> > variable with that value.
> >
> > Whatever I try, I cannot get the shell to evaluate some expression and
> use
> > the result as a variable name. Thus for instance
> >    varvar=var
> >    $varvar=val
> > results in
> >    var=val: command not found
> >
> > In other words, can I obtain the name of a name of a variable from
> another
> > variable?
>
> Example:
>
> some_variable="hello world"
> another_text="goodbye world"
> my_variable_name=some_variable
>
> # Read variable
> echo ${!my_variable_name}
> # Assign variable
> eval "$my_variable_name=\"\$another_text\""
> # Read variable again
> echo ${!my_variable_name}
>
> More:
> http://tldp.org/LDP/abs/html/ivr.html
>
> Ew. Why not just use printf -v "$my_variable_name" %s "$another_text" ?


reply via email to

[Prev in Thread] Current Thread [Next in Thread]