How can I pipe stderr, and not stdout?

后端 未结 11 1378
-上瘾入骨i
-上瘾入骨i 2020-11-22 03:28

I have a program that writes information to stdout and stderr, and I need to process the stderr with grep, leaving

11条回答
  •  生来不讨喜
    2020-11-22 04:15

    Or to swap the output from standard error and standard output over, use:

    command 3>&1 1>&2 2>&3
    

    This creates a new file descriptor (3) and assigns it to the same place as 1 (standard output), then assigns fd 1 (standard output) to the same place as fd 2 (standard error) and finally assigns fd 2 (standard error) to the same place as fd 3 (standard output).

    Standard error is now available as standard output and the old standard output is preserved in standard error. This may be overkill, but it hopefully gives more details on Bash file descriptors (there are nine available to each process).

提交回复
热议问题