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

Re: lines to array

"Christian Walther" <cptsalek@xxxxxxxxx> wrote:
> On 14/02/07, Matt Wozniski <godlygeek@xxxxxxxxx> wrote:
> > array=( "${(f)$(ls -l)}" )
> Could you please explain what ${(f)...} actually does? Is there some
> kind of parameter expansion going on?

Yes, the effect of (f) is described in "Parameter Expansion Flags" in the
zshexpn manual page.  There's a hairy description below that of how nested
parmaeter expansion works.  This counts as a nested parameter expansion
even though the innermost level is actually a command substitution.

This gets explained every now and then on this list, but to recap:

$(...) inside parameter is treated as a normal command substitution.  The
text for enclosing parameter expansion operations is the result of the
command substitution rather than taken from a parameter.  Because of the
double quotes, the command substitution produces a single word at this

(f) is documented as being equivalent to (ps:\n:).  The s:\n: forces the
text to be split on \n. the p interprets the \n as a newline according to
normal "print" rules.

The result is the text of "ls -l", split into lines.

You'll often see this written as

array=( ${(f)"$(ls -l)"} )

which indicates explicitly the only the command substitution is affected by
the double quotes.  However, forced splitting (unlike automatic word
splitting) overrides the double quotes anyway.

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

To access the latest news from CSR copy this link into a web browser:  http://www.csr.com/email_sig.php

To get further information regarding CSR, please visit our Investor Relations page at http://ir.csr.com/csr/about/overview

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