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

Re: Word splitting with command substitution



Omari Norman wrote:
> % echo $(head -n 2 /etc/passwd)
> root:x:0:0:root:/root:/bin/bash daemon:x:1:1:daemon:/usr/sbin:/bin/sh
> 
> % contents=$(head -n 2 /etc/passwd)
> % echo $contents
> root:x:0:0:root:/root:/bin/bash
> daemon:x:1:1:daemon:/usr/sbin:/bin/sh
> 
> Why does the second example give me two separate lines, while the
> first is joined into one line?

In the second case the assignment is to a scalar, so is necessarily a
single word, hence there is no splitting and the newlines are left
intact.  If you want multiple words, assign to an array:

contents=($(head -n 2 /etc/passwd))

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


.



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