Gentoo Websites Logo
Go to: Gentoo Home Documentation Forums Lists Bugs Planet Store Wiki Get Gentoo!
Bug 356403 - app-shells/bash-4.1_p7 bad double-quoted pattern substitution with arrays
Summary: app-shells/bash-4.1_p7 bad double-quoted pattern substitution with arrays
Status: RESOLVED INVALID
Alias: None
Product: Gentoo Linux
Classification: Unclassified
Component: [OLD] Core system (show other bugs)
Hardware: AMD64 Linux
: High minor
Assignee: Gentoo's Team for Core System packages
URL: http://lists.gnu.org/archive/html/bug...
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-02-25 12:00 UTC by Diego Augusto Molina
Modified: 2011-03-01 14:58 UTC (History)
0 users

See Also:
Package list:
Runtime testing required: ---


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Diego Augusto Molina 2011-02-25 12:00:28 UTC
It's difficult to explain. Better see below the particular case I had.

I needed to assign the elements of an array to other, but with a preceding single quote. The following is a simplified (yet illustrative) example.

declare -a array1=(a b c d e f) array2=()
array2=( "${array1[@]/#/'}" )

AFAIK (plus the man page), the syntax of pattern substitution is ${parameter/pattern/string}, where "string" is just that, a string. I have also tried the following:

array2=( "${array1[@]/#/"'"}" )

But that caused a literal preceding "'", which technically is ok.
The ugly solution I had to take was a for loop. Nothing stressing but bothers.

Reproducible: Always

Steps to Reproduce:
1.declare -a my_array=(a b c d e)
2.echo "${my_array[@]/#/'}"

Actual Results:  
Nothing actually, just the PS2 wating for a closing single quote.

Expected Results:  
'a 'b 'c 'd 'e 'f

BASH shouldn't have treated specially the single quote after the slash and before the closing brace.
Comment 1 Ulrich Müller gentoo-dev 2011-02-27 07:22:57 UTC
Possible workaround:

$ q="'"
$ echo "${my_array[@]/#/$q}"
'a 'b 'c 'd 'e
Comment 2 Diego Augusto Molina 2011-02-27 08:26:24 UTC
(In reply to comment #1)
> Possible workaround:
> 
> $ q="'"
> $ echo "${my_array[@]/#/$q}"
> 'a 'b 'c 'd 'e
> 

Thanks a lot Ulrich, I didn't thought about that. It really simplifies the code.
I have sent a bug report to the developers.
Comment 3 Diego Augusto Molina 2011-03-01 14:58:56 UTC
Chet Ramey answered my e-mail (reporting the bug) and here's what he said:

>Maybe.  However, bash has always treated embedded quoted strings as
>introducing a new `quoting context', even within double quotes.  This
>has occasionally resulted in awkward constructs, of which this is
>one.
>
>FWIW, of the shells with arrays I had handy to test, ksh93 and mksh do
>the same thing.  zsh behaves as you prefer.

I assume this behaviour is intentional. Thus, closing the bug as invalid.