Search code examples
bashstdoutstderrpostfix-mta

stderr can only be printed on screen but cannot be piped


I just encountered something strange about stdout and stderr!

This works perfectly:

$ ( { { echo "This is stdout"; echo "This is stderr" 1>&2; } | sed "s/^/stdout captured - /"; } 3>&2 2>&1 1>&3 ) | sed "s/^/stderr captured - /"
stderr captured - This is stderr
stdout captured - This is stdout

However, see this:

$ sudo postfix status >/dev/null
postfix/postfix-script: the Postfix mail system is not running
$ sudo postfix status 2>/dev/null
$

I think, definitely the message on screen is STDERR, BUT:

$ ( { { sudo postfix status; } | sed "s/^/stdout captured - /"; } 3>&2 2>&1 1>&3 ) | sed "s/^/stderr captured - /"
$

Nothing captured. I simply issue below command and get nothing again:

$ `sudo postfix status 2>/tmp/somelog` && cat /tmp/somelog
$

It seems beyond my knowledge about bash

ps: I'm on MAC OSX 10.11.6, terminal.


Solution

  • I think this has to do with the postfix command actually behaving differently when it is part of a pipe.

    I tried this on my system :

    sudo postfix status 2>log
    

    And log turns out empty. Same thing with :

    postfix status 2>log
    

    It seems postfix suppresses this output when its output is not going to a terminal.