开发者

What's the difference between foo | head and foo | & head?

foo | head and foo | & 开发者_如何学编程head: what's the role of & in the latter case? I know that & is used to kick a process to the background, but both the commands yielded the same results when I tested it out.


It redirects the standard error as well as the standard output of foo into the standard input of head. Otherwise standard error would still be printed on the screen, instead.

With the space between the pipe and the ampersand, foo | & head works in csh and derived shells, but is a syntax error in sh derived shells. Without the space, foo |& head is shorthand in some sh derived shells (zsh, bash, possibly others) for foo 2>&1 | head, but fails in many others.


You likely mean |& with no space, as | & with a space is a syntax error (or, as far as I can tell, the & is a noop in csh).

|& is a synonym for 2>&1 |, which means to redirect file descriptor 2 (standard error) to file descriptor 1 (standard out) before piping standard out to the next command in the pipeline. This allows you include anything printed to standard error in the pipeline, instead of errors being printed to the terminal as they would normally. This is supported by a few shells, such as Bash. See the Bash Manual for more information.

Note that this syntax was only introduced to Bash in Bash 4 (it's been in a few other shells for a while), and is not POSIX compatible, so don't try to use it in portable shell scripts.


IIRC yes thats all it does is tell head to run as a background process. The result SHOULD be identical (otherwise &) is changing the semantics of the command), it is just the scheduling that is affected.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜