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

Re: read stdin in variable howto ?



On Tue, Jan 31, 2006 at 02:45:48PM +0100, Marc Chantreux wrote:
> le 31/01/2006,
> Vincent Lefevre nous écrivait :
> > $(<&0) also works.
> 
> exactly kind of answer i expected! thanks a lot Vincent!
[...]

But it does the same thing as $(cat) (it does call cat after a
pipe and a fork through the $NULLCMD mechanism), contrary to $(<
/dev/stdin) which uses the $(< file) special operator.

In $(<&0), it's command substitution, and $NULLCMD <&0. So the
shell forks a "cat" process whose stdout is set to a pipe. the
parent process reads the other end of the pipe to build the
string.

In $(< /dev/stdin), the shell reads the /dev/stdin file by
itself in the current process (no fork, no pipe). /dev/stdin (or
/dev/fd/0 or /proc/self/fd/0) is a special feature of some
operating systems, which on some systems (like Solaris) makes
that an open("/dev/stdin") is equivalent to a dup(0), and on
others (like Linux) opens the same resource as the one pointed
to by the fd 0 of the process (beware that some shells like bash
or ksh have a confusing feature where they emulate that OS
mechanism (well in the Solaris way) (in some circumstancies)).

Note that in both cases, the trailing empty lines are removed.
That's a bug/feature common to all the Bourne like shells.

-- 
Stéphane



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