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

Re: $^=var and leading/trailing blanks

On Sat, Oct 8, 2016 at 8:12 AM, Stephane Chazelas
<stephane.chazelas@xxxxxxxxx> wrote:
> Why the extra first and last element the the first two commands?

This is sort of explained under the s:: expansion flag:

     For historical reasons, the usual behaviour that empty array
     elements are retained inside double quotes is disabled for arrays
     generated by splitting; hence the following:

          print -l "${(s.:.)line}"

     produces two lines of output for one and three and elides the
     empty field.  To override this behaviour, supply the "(@)" flag as
     well, i.e.  "${(@s.:.)line}".

When you add the rcexpandparam flag, you get "the usual behavior" back
again (otherwise an array of only empty elements would behave like an
empty array and discard everything).  When you  nest the expansions,
the inner split gets the empty elements removed when it expands, and
then rcexpandparam applies to the remaining elements of that result.

You don't get two elements for the two leading spaces because
shwordsplit behaves that way.  If you use (s: :) instead, then you do
get one empty element for each space.

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