The new feature, associative arrays in bash 4.0 was very useful but I
can't find any way to declare them as global when declaring inside a
function. By default all variables are set local when using declare
and typeset and there are no other ways to declare an associative
array but through the said functions.
I hope the development team will also consider adding a way in bash to
declare global variables inside a function perhaps either with an
option in typeset or declare like -g (same as zsh) and/or a builtin
function like global as similar to local.
Cheers,
konsolebox
I thought variables in functions were /always/ global unless declared local?
--
Matthew
Please do not quote my e-mail address unobfuscated in message bodies.
--
"[Microsoft's] license tells you what a netbook is." -- Steve Ballmer
It's the other way round. Regarding typeset and declare, the man page
says: " When used in a function, makes each name local, as with the
local command. " So within a function, typeset, declare, and local are
synonyms. Using 'local' outside a function is an error, so IMHO this
command is completely redundant. It _would_ make some sense, however, if
its counterpart 'global' existed, as it could help clarify the intended
usage of the variable.
Bernd
--
Bernd Eggink
http://sudrala.de
> I hope the development team will also consider adding a way in bash to
> declare global variables inside a function perhaps either with an
> option in typeset or declare like -g (same as zsh) and/or a builtin
> function like global as similar to local.
I second that. I'm missing this feature badly. Presently, you can only
get around this restriction by using a lot of ugly and dangerous 'eval's.
> Am 12.12.2009 02:11, schrieb Matthew Woehlke:
> > konsolebox wrote:
> > > I hope the development team will also consider adding a way in bash to
> > > declare global variables inside a function perhaps either with an
> > > option in typeset or declare like -g (same as zsh) and/or a builtin
> > > function like global as similar to local.
> >
> > I thought variables in functions were /always/ global unless declared
> > local?
>
> It's the other way round. Regarding typeset and declare, the man page says: "
> When used in a function, makes each name local, as with the local command. "
> So within a function, typeset, declare, and local are synonyms. Using 'local'
> outside a function is an error, so IMHO this command is completely redundant.
> It _would_ make some sense, however, if its counterpart 'global' existed, as
> it could help clarify the intended usage of the variable.
Of the three, local is the only command I use. It says exactly what
it does.
--
Chris F.A. Johnson, webmaster <http://woodbine-gerrard.com>
===================================================================
Author:
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
Pro Bash Programming: Scripting the GNU/Linux Shell (2009, Apress)
For information, zsh has the -g typeset option for that.
--
Stᅵphane
To avoid misunderstandings, let me add that you are right (only) with
respect to variables being used _without_ declaration. The problem is
that associative arrays are the only kind of variables that _must_ be
declared, since something like "a[foo]=bar" without a preceding
declaration would create an indexed array (and set a[0] to "bar").
$ vars () { var1=123; local var2=456; }
$ vars
$ echo "var1=$var1 "var2=$var2"
var1=123 var2=
How is that "the other way around"?
(Sorry, something went wrong with the last reply)
What I meant is that variables _declared_ in functions are always global.
> It's the other way round. Regarding typeset and declare, the man page
> says: " When used in a function, makes each name local, as with the
> local command. " So within a function, typeset, declare, and local are
> synonyms. Using 'local' outside a function is an error, so IMHO this
> command is completely redundant. It _would_ make some sense, however, if
> its counterpart 'global' existed, as it could help clarify the intended
> usage of the variable.
It might be considered redundant, but just consider it a more mnemonic
synonym for declare/typeset. `local' appeared in an earlier version of
the Posix standard, though `typeset' never did.
Chet
--
``The lyf so short, the craft so long to lerne.'' - Chaucer
``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, ITS, CWRU ch...@case.edu http://cnswww.cns.cwru.edu/~chet/
"local" is also mandated by the LSB spec for sh as well (Linux
standard base). So you can reasonably expect to be able to use
it (though only in "local varname") on any script meant for a
recent Linux sh.
--
Stᅵphane
OK. I'm interested in use cases for this feature. What are you trying
to do that you're missing it "badly?"
One example is introducing some kind of data encapsulation in bash, or
even a modest form of object orientation. You may laugh at it, but I
think it might be useful in complex scripts. I experimented a little and
came up with 'classes' consisting of a function, and 'objects'
consisting of a function (representing the interface) plus an equally
named associative array (containing the data). The difficulties arise if
you want to create an object 'a' of class X by saying
X create a
You can declare a function 'a' dynamically within function X (using
eval), but you can't declare a (global) associative array 'a' there. So
that's where a global declaration would be desirable. The alternative is
to print the declarations and eval the whole thing in the current
environment, but that is very inconvenient and can cause nasty quoting
problems.
Regards,
Bernd
OK, but not having to explicitly "declare" variables is a feature that
most people expect from dynamic languages, so you can hardly blame them
for doing this all the time in shell.
IMHO Python gets this global/local (implicit) declaration story
right. Of course it is easier when you do not have 30 years of POSIX
legacy to support (but just the experience).
<http://docs.python.org/tutorial/controlflow.html#defining-functions>
I don't blame anybody. I'm talking about the fact that associative
arrays are an exception from "what most people expect from dynamic
languages", as they _have_ to be explicitly declared. This doesn't
bother me, but I would prefer to have a choice between local and global
declaration anyway.