According to "Linux: The Complete Reference 6th Edition" (pg. 44), you can pipe only STDERR using the |&
redirection symbols.
I've written a pretty simple script to test this:
#!/bin/bash
echo "Normal Text."
echo "Error Text." >&2
I run this script like this:
./script.sh |& sed 's:^:\t:'
Presumably, only the lines printed to STDERR will be indented. However, it doesn't actually work like this, as I see:
Normal Text.
Error Text.
What am I doing wrong here?
./script.sh > /tmp/stdout_goes_here |& grep 'grepping_script_stderr'
doesn't work as intended, ie: redirectscript.sh
'sstdout
(which, according to the manual snippet should happen first), then allowgrep
to process the script'sstderr
. Instead,stderr
and tdoutboth end up in
stdout_goes_here` – sxc731 Nov 11 '19 at 13:05|&
is shorthard for2>&1 |
. So>/tmp/stdout_goes_here |&
redirects stdout to/tmp/stdout_goes_here
, then2>&1
redirects stderr to wherever stdout is going, which is/tmp/stdout_goes_here
, and finally|
doesn't receive any input because the output of the command has been redirected. Keep in mind that>&1
redirects to wherever file descriptor 1 is currently going, not to wherever file descriptor 1 will end up going. To pipe stderr only and redirect stdout to a file, one way is2>&1 >/tmp/stdout_goes_here |
. – Gilles 'SO- stop being evil' Nov 11 '19 at 23:29