Piping both stdout and stderr in bash?

后端 未结 2 1472
盖世英雄少女心
盖世英雄少女心 2020-11-28 03:51

It seems that newer versions of bash have the &> operator, which (if I understand correctly), redirects both stdout and stderr to a file (&>

2条回答
  •  情深已故
    2020-11-28 04:14

    (Note that &>>file appends to a file while &> would redirect and overwrite a previously existing file.)

    To combine stdout and stderr you would redirect the latter to the former using 2>&1. This redirects stderr (file descriptor 2) to stdout (file descriptor 1), e.g.:

    $ { echo "stdout"; echo "stderr" 1>&2; } | grep -v std
    stderr
    $
    

    stdout goes to stdout, stderr goes to stderr. grep only sees stdout, hence stderr prints to the terminal.

    On the other hand:

    $ { echo "stdout"; echo "stderr" 1>&2; } 2>&1 | grep -v std
    $
    

    After writing to both stdout and stderr, 2>&1 redirects stderr back to stdout and grep sees both strings on stdin, thus filters out both.

    You can read more about redirection here.

    Regarding your example (POSIX):

    cmd-doesnt-respect-difference-between-stdout-and-stderr 2>&1 | grep -i SomeError
    

    or, using >=bash-4:

    cmd-doesnt-respect-difference-between-stdout-and-stderr |& grep -i SomeError
    

提交回复
热议问题