On Thu, Aug 18, 2011 at 08:03:41AM -0700, Linda Walsh wrote: > >4.2 introduce a new -g to declare a global variable inside a function. > ---- > Which doesn't say what it would do in situations like the above.
Then let's test: imadev:~$ echo $BASH_VERSION 4.2.10(7)-release imadev:~$ unset a b; unset -f f1 f2 imadev:~$ f1() { local a='tmp'; f2; } imadev:~$ f2() { declare -g a b; a=3 b=5; } imadev:~$ a=a b=b; f1; echo "a=<$a> b=<$b>" a=<a> b=<5> It would appear "declare -g" does NOT allow you to "jump over" a local variable that is shadowing a global. That's disappointing. > When I asked about defining a local var in a command in the context > of the caller, -- greg didn't even think of aliases, just inferred it > couldn't be done except by doing it in the caller -- i.e. manually > typing it in each time.. Not good! If you're going to use the "magic aliases" or "upvar" hacks in your scripts, please don't ever ask me to debug them. Be fair -- your question did not ask how to do "call by reference" in bash. You asked a very specific thing, and I gave a very specific answer and example. If you had wanted arbitrary call-by-reference then I would have pointed you back to FAQ 6. If you want call-by-ref for arrays, then I will repeat my recommendation that you use ksh93 which has an actual language feature for this, not bash, which doesn't. It's not that I "didn't think of" things. It's that I *have* thought about them in the past several years, and I know what is reasonable and what is not. Certain things cannot be achieved reasonably in bash, and so we *don't do them* in bash, and we advise other people not to do them either. Does this mean they're impossible? Maybe not. But it means they are not a good idea! We have all these other languages we could use instead, where these tasks *can* be achieved reasonably. When you run into such a task, it makes much more sense to switch to a language that's suited to it, rather than using arcane trickery.