help-bash
[Top][All Lists]
Advanced

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

Re: [Help-bash] Specifying multiple index numbers in array


From: Greg Wooledge
Subject: Re: [Help-bash] Specifying multiple index numbers in array
Date: Mon, 6 May 2013 08:35:59 -0400
User-agent: Mutt/1.4.2.3i

On Fri, May 03, 2013 at 03:55:42PM -0400, Chris F.A. Johnson wrote:
>   Use brace expansion:
> 
> echo "${FOO["{2,4}"]}"
> 
>   Note that the brace expression is not quoted.

My rule of thumb is any time I see a bit of code and have to wonder
whether it will work or not, it's too complicated, and needs to be
simplified.

In the absence of any bugs (see Dan Douglas's message in this thread),
this is just an obfuscated way of writing:

echo "${FOO[2]}" "${FOO[4]}"

In this particular case, the ambiguity is in how the quotes are handled.
Are the two quotes inside the curly braces considered to be nested and
handled as such, as in:

"${assoc_array["$index"]}"

Or does the brace expansion take precedence, and force the parser to
chop the expression in two, with a pair of quotes on the left and
another pair of quotes on the right?

Does it matter whether FOO is declared as an indexed array, or an
associative array?  Does the parser's decision vary across different
versions of bash, or different shells?

I bet some of you remember this one:

cat < <(echo {1,2})

Try that in a few different versions of bash and watch the fun.

I advise that you avoid brace expansions in scripts, period.  It never
adds any utility; all it can ever do, in the best case, is save a few
bytes of storage.  And it does this at the cost of making the code harder
to read, and more likely to fail.



reply via email to

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