4

The ${parameter,,pattern} parameter expansion converts alphabetic characters in parameter to lower-case.

On cygwin 1.7.11-1 Bash 4.1.10(4) and also on my debian squeeze Bash 4.1.5(1);
if i run the following, i get a curious result:

$ declare -a a=(Zero One Two Three); n=0; echo "${a[n],,}->${n}"; echo "${a[++n]}->${n}"; echo "${a[++n],,}->${n}"
zero->0
One->1
three->3
$

NB: similar results happen:
for ,,* or ^^ case conversion;
for some other expansions such as ${parameter##word};
for using either prefix/postfix ++ or -- operator;
for using $((++n)) instead of just ++n.

However, the length expansion ${#parameter} works as i might expect:
in the above snippet, echo "${#a[++n]}->${n}" instead of echo "${a[++n],,}->${n} would yield 3->2 instead of three->3 ~& the length of a[2]="two" is indeed 3 characters.


I imagine that the parameter expansion is happening twice. But why is this happening?

5
  • Do you mean why the re-order of values on array iteration?
    – Jé Queue
    Apr 8, 2012 at 18:43
  • i mean exactly that i would expect to get returned:zero->0 One->1 two->2
    – violet313
    Apr 8, 2012 at 19:27
  • +1 for a well-investigated question, but I don't think you'll get a good answer, because I think it's simply a bug. On my system, Bash crashes (!) if I add another "${a[++n],,}" that should move to the last element but would (per your results) move one past the end of the array.
    – ruakh
    Apr 8, 2012 at 20:58
  • By the way, if you can confirm that it happens in the latest version of Bash, and if no one offers a theory for how it could be anything other than a bug, gnu.org/software/bash/manual/html_node/Reporting-Bugs.html has instructions for how to report it.
    – ruakh
    Apr 8, 2012 at 20:59
  • ruakh, agreed. it probably should be flagged as a bug -since as you say it can be made to seg-fault. i am grabbing the bash source now & will raise a report once i've taken a look inside
    – violet313
    Apr 9, 2012 at 9:30

2 Answers 2

1

I grabbed the latest bash source & it appears that this issue is resolved for this version:

$ ./bash --version
GNU bash, version 4.2.24(1)-release (x86_64-unknown-linux-gnu)
Copyright (C) 2011 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
$

Note however that on my Debian Squeeze stable, I have:

$ apt-cache policy bash
bash:
  Installed: 4.1-3
  Candidate: 4.1-3
  Version table:
 *** 4.1-3 0
        500 http://ftp.uk.debian.org/debian/ squeeze/main amd64 Packages
        100 /var/lib/dpkg/status
$
$ bash --version
GNU bash, version 4.1.5(1)-release (x86_64-pc-linux-gnu)
Copyright (C) 2009 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>


I think this means that currently on Debian Squeeze, the latest stable version of bash is 4.1.5(1) & contains this bug. Also on Cygwin 1.7.11-1 currently the latest available bash package appears to be version 4.1.10(4) & contains this bug. Of course the issue is easy to work around, so no real need to build the 4.2 sources.

0

I got

Zero->0
One->1
Two->2

However I think "${a[++n]}->${n}" may not be well defined.

try substitute with

let n=$n+1 ; echo "${a[n],,}->${n}";

To inject the notion of sequence.

2
  • if you look closely at your results, you will notice that neither the Zero nor the Two have been converted to lower-case. Now try instead with echo "${a[++n]foo}->${n}" same results!. At this point you might want to check your version of bash. For example, on Bash 3.2.25(1) i get the above results also ;)
    – violet313
    Apr 9, 2012 at 8:54
  • The lowercasing expression is not valid in bash 3.
    – pizza
    Apr 9, 2012 at 19:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.