[Toybox] redirect vs pipe order.
Chet Ramey
chet.ramey at case.edu
Fri Sep 4 19:27:31 UTC 2026
On 9/3/26 6:05 PM, Rob Landley wrote:
> Redrecting stdout wins over | but redirecting stderr loses to |&
>
> $ echo hello > /dev/null | cat
> $ cat nothing 2>/dev/null
> $ cat nothing 2>/dev/null |& cat
> cat: nothing: No such file or directory
>
> Why?
You can pretty much suss it out from the man page:
"If |& is the pipeline
operator, command1's standard error, in addition to its standard output, is
connected to command2's standard input through the pipe; it is shorthand
for 2>&1 |."
So the commands turn into
echo hello > /dev/null | cat # piping takes place before redirection
cat nothing 2>/dev/null # just stderr redirection
cat nothing 2>/dev/null 2>&1 | cat # piping before redirection
Last redirection of fd 2 wins, 2>&1 trumps 2>/dev/null, stderr goes through
the pipe.
And indeed, this is explicitly stated in the man page:
"This implicit redirection of the standard error to the stan-
dard output is performed after any redirections specified by command1."
--
``The lyf so short, the craft so long to lerne.'' - Chaucer
``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRU chet at case.edu http://tiswww.cwru.edu/~chet/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: OpenPGP_signature.asc
Type: application/pgp-signature
Size: 203 bytes
Desc: OpenPGP digital signature
URL: <http://lists.landley.net/pipermail/toybox/attachments/20260904/66dd17c1/attachment.sig>
More information about the Toybox
mailing list