zsh-users
 help / color / mirror / code / Atom feed
* Word splitting with command substitution
@ 2007-09-20 16:33 Omari Norman
  2007-09-20 16:56 ` Peter Stephenson
  0 siblings, 1 reply; 2+ messages in thread
From: Omari Norman @ 2007-09-20 16:33 UTC (permalink / raw)
  To: zsh-users

% 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? The zshexpn man page says that
unquoted command substitutions are split into words, so I would
think the first case is correct, but why does the second case work
as it does?

-- 
A new dinette set!


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: Word splitting with command substitution
  2007-09-20 16:33 Word splitting with command substitution Omari Norman
@ 2007-09-20 16:56 ` Peter Stephenson
  0 siblings, 0 replies; 2+ messages in thread
From: Peter Stephenson @ 2007-09-20 16:56 UTC (permalink / raw)
  To: zsh-users

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@csr.com>                  Software Engineer
CSR PLC, Churchill House, Cambridge Business Park, Cowley Road
Cambridge, CB4 0WZ, UK                          Tel: +44 (0)1223 692070


.


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2007-09-20 16:57 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-09-20 16:33 Word splitting with command substitution Omari Norman
2007-09-20 16:56 ` Peter Stephenson

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).