help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-bash] starting a central bash script repository


From: Dan Douglas
Subject: Re: [Help-bash] starting a central bash script repository
Date: Sun, 07 Apr 2013 16:42:24 -0500
User-agent: KMail/4.8.3 (Linux/3.4.6-pf+; KDE/4.8.3; x86_64; ; )

On Sunday, April 07, 2013 04:17:43 PM Chris F.A. Johnson wrote:
>     Just because it's valid code doesn't mean it's the right way to do
>     it.
> 
>     But is it even valid code?
> 
>     man bash:
>       typeset
>         ...
>         -n ...
>            The -n attribute cannot be applied to array variables.

That means you can't have an array of namerefs. It doesn't mean that you can't 
have a reference variable which points to an array variable, as in this case. 
It would defeat much of the purpose if you couldn't. In ksh93 you would use a 
compound variable (struct) to hold a collection of pointers, which is why that 
restriction exists in that shell.
 
> 
>    In any case, whatever you are trying to do, I'm sure it can be done
>    perfectly safely in bash.

It can't. In this example we're passing pointers through multiple calls which 
contain namespace conflicts. Since this particular example is all one 
function, it isn't much of a problem because the author of the function can 
easily work around it.

The problem occurs when crossing library boundaries in which the caller has no 
knowledge of the internals of the function being called. There's no easy 
workaround in Bash.

>    And it's not even valid in ksh93 (remove the unnecessary
>    'typeset -a' and it would be).

The code is correct. It is necessary to use typeset to localize the variable, 
and the -a is a good idea to make it indexed rather than associative. Ksh 
arrays are not necessarily indexed by default (if keys were specified in a 
compound assignment).

-- 
Dan Douglas



reply via email to

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