Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Bug in parameter expansion flag 'o'?

On Tue, 11 May 2010 16:37:05 +0100
Nick Pope <nick@xxxxxxxxxxxxxx> wrote:
> It seems to be that there is a possible bug in the parameter
> expansion flag 'o' when the variable is referenced in a string:
>      $ x=(a c b); echo ${(o)x}; echo "${(o)x}"
>      a b c
>      a c b
> It should be noted that other flags still work alongside:
>      $ x=(a c b); echo ${(Uo)x}; echo "${(Uo)x}"
>      A B C
>      A C B
> Is this because arrays are being incorrectly converted to string
> prior to getting around to the sorting?

No, it's because arrays are correctly being converted to a string prior to
getting around to the sorting.  Transformations specified by flags are done
late on in the expansion because typically they need the result of any of
the basic operations such as joining and splitting.  It so happens in this
case the effect isn't particularly helpful.  The U flag doesn't care
whether it's operating on a string or an array.

The order of operations at each level of parameter expansions is documented
at the end of the PARAMETER EXPANSION section of zshexpn; here's an
extract.  Note in particular the disclaimer.

       Here  is  a  summary  of  the rules for substitution; this assumes that
       braces are present around the substitution, i.e. ${...}.  Some particu-
       lar  examples  are  given  below.   Note that the Zsh Development Group
       accepts no responsibility for any brain damage which may  occur  during
       the reading of the following rules.


       5. Double-Quoted Joining
              If  the  value after this process is an array, and the substitu-
              tion appears in double quotes, and no (@) flag is present at the
              current  level, the words of the value are joined with the first
              character of the parameter $IFS, by  default  a  space,  between
              each  word  (single  word  arrays are not modified).  If the (j)
              flag is present, that is used for joining instead of $IFS.


       12. Ordering
              If  the  result  is still an array and one of the â(o)â or â(O)â
              flags was present, the array is reordered.

As you'll see from where things happen in that list, for once the shell is
just being consistent.  This admittedly unusual phenomenon makes the
maintainer's job easier (though in the case of the particular piece of C
code that implements parameter substitution, alas, not very much).

I realise you may need to quote the result for some other reason; if you
can tell us what it is there may be a simple workaround.  Obviously there's
an obvious less simple workaround of assigning to a sorted array before

Peter Stephenson <pws@xxxxxxx>            Software Engineer
Tel: +44 (0)1223 692070                   Cambridge Silicon Radio Limited
Churchill House, Cambridge Business Park, Cowley Road, Cambridge, CB4 0WZ, UK

Member of the CSR plc group of companies. CSR plc registered in England and Wales, registered number 4187346, registered office Churchill House, Cambridge Business Park, Cowley Road, Cambridge, CB4 0WZ, United Kingdom

Messages sorted by: Reverse Date, Date, Thread, Author