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: Marco Ippolito
Subject: Re: [Help-bash] taking the name of a variable from another variable
Date: Tue, 3 Jul 2018 16:29:41 +0200
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.8.0

On 03/07/18 15:57, Daniel Mills wrote:

> 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" ?

Right, he could be using printf -v there (nicer syntax), but he
mentioned "an environment variable" so I might add an `export' after
that or just a `declare -x <parameter_name>', to the same effect.



reply via email to

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