Is "2nd level indirection" possible in bash?

Chris F.A. Johnson chris-E7bvbYbpR6jSUeElwK9/Pw at public.gmane.org
Thu Aug 30 20:33:51 UTC 2012


On Thu, 30 Aug 2012, Ben Walton wrote:

> Hi Walter,
>
>> # Routine to assemble date in YYYYMMDD format, using column locations
>> # imported in format file (i.e. 2nd parameter on commandline)
>> calc_yyyymmdd() {
>>    yyyymmdd="${dataline:${f_yr}}${dataline:${f_mo}}${dataline:${f_dy}}"
>>    export yyyymmdd
>> }
>
> I think this is what you're after (assuming I understand what you're
> trying to do):
>
> ME=ben
> varname=ME
> echo ${!varname}
>
> You build up the variable you're after then then use the ! to
> dereference the name.

    In shells that don't support the indirect expansion, use eval:

eval echo "\$$varname"

-- 
    Chris F.A. Johnson, <http://cfajohnson.com/>
    Author:
    Pro Bash Programming: Scripting the GNU/Linux Shell (2009, Apress)
    Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
--
The Toronto Linux Users Group.      Meetings: http://gtalug.org/
TLUG requests: Linux topics, No HTML, wrap text below 80 columns
How to UNSUBSCRIBE: http://gtalug.org/wiki/Mailing_lists





More information about the Legacy mailing list