[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Help-bash] Chain/cascade assignment
From: |
Greg Wooledge |
Subject: |
Re: [Help-bash] Chain/cascade assignment |
Date: |
Mon, 22 Feb 2016 14:32:04 -0500 |
User-agent: |
Mutt/1.4.2.3i |
On Mon, Feb 22, 2016 at 01:18:14PM -0600, Dennis Williamson wrote:
> This is hackish, probably has some gotchas and requires a recent version of
> Bash for the -g global option of declare. So use at your own risk.
>
> assign () { val=$1; shift; for var; do declare -g $var=$val; done; }
>
> $ assign 42 a b c d[4] e[{3..8}]
Gotchas that I can see:
1) You must quote 'd[4]' and so on, to avoid glob expansion if there happens
to be a file named d4.
2) declare -g forces the variable into the global namespace, whether you
wanted that or not. There's no way to assign the values to variables
which are local to the caller (or some other n'th generation ancestor
which is not the global level).
What you really want here is something akin to Tcl's upvar. Freddy
Vulto did some research on this and came up with:
http://www.fvue.nl/wiki/Bash:_Passing_variables_by_reference
Suffice to say, this is one of the areas where Bash does not shine.