bug-bash
[Top][All Lists]
Advanced

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

Re: Severe Bash Bug with Arrays


From: Linda Walsh
Subject: Re: Severe Bash Bug with Arrays
Date: Thu, 26 Apr 2012 20:47:39 -0700
User-agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.8.1.24) Gecko/20100228 Lightning/0.9 Thunderbird/2.0.0.24 Mnenhy/0.7.6.666



Maarten Billemont wrote:

On 26 Apr 2012, at 01:18, Linda Walsh wrote:
Ishtar:> echo "${b[*]}"

Please note that expanding array elements using [*] is usually NOT what anyone 
wants.



---
It was exactly what I wanted for my example to work.  Please don't suggest
non-working solutions to the example code I provided.  ${b[@]}  Will NOT
work as a replacement for [*].  It isn't not BETTER, it is DIFFERENT.
That's why there are both.

your array like above, you then follow up by re-splitting the whole thing using standard wordsplitting, not to mention pathname expanding the resulting words.



----
I followed up?  I don't understand this.  I gave a progressive example
that showed the results at each step.  It wasn't a canned solution for
the poster's problem, it was a different method of looking at the problem
that they could apply using whatever modifications they would need for their
example.   Obviously, they were not complete beginners to bash -- to be reading
arrays in from vars holding multi-line text?   I would assume they'd have the
intelligence to know when to use * or @ and I wouldn't have to talk down to them
and cover basics.


"${b[@]}" # Expands each array element as a separate argument,

            protecting it from wordsplitting and pathname expansion mutilation.

${b[@]} # Identical to and just as broken as ${b[*]}.  Never do this.

----
No.    _NEVER_ USE ${b[@]} when you want the IFS inserted between
elements.   It doesn't work.  (I wish BASH had an OFS, that worked for
such cases as well as other expansions), but with the given system, '@' is
totally unacceptable to suggest when one wants to create an expression with
braces that bash can automatically expand.

You should always recommend the "${b[@]}" variant.

----
I shouldn't ALWAYS do anything.  It's dangerous.
You can do so for your examples... It won't work in the next
two any more than the last one of the previous...


sort an arg list:

> args="-dpi -xinerama -clipboard -unixkill -nowinkill -ac +bs -fp -multiwindow"

> readarray -t dflts< <( a=( $args ); IFS=$'\n'; echo "${a[*]#?}"|sort -k1.2 )

> printf "  defaults=(%s)" "${dflts[*]}"

defaults=(-ac +bs -clipboard -dpi -fp -multiwindow -nowinkill -unixkill -xinerama)

Want to create pathnames using bash's /path/{}/  feature and have the paths
in an array (for simple paths):

> a=(lib tmp bin share)
> (export IFS=,;eval "echo /usr/{${a[*]}}")
/usr/lib /usr/tmp /usr/bin /usr/share



Anything else you wanna tell me NEVER/ALWAYS to do?

(contrarian)



reply via email to

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