How to redirect and append both stdout and stderr to a file with Bash?

前端 未结 7 1161
春和景丽
春和景丽 2020-11-22 01:16

To redirect stdout to a truncated file in Bash, I know to use:

cmd > file.txt

To redirect stdout in Bash, appending to

7条回答
  •  你的背包
    2020-11-22 01:33

    cmd >>file.txt 2>&1
    

    Bash executes the redirects from left to right as follows:

    1. >>file.txt: Open file.txt in append mode and redirect stdout there.
    2. 2>&1: Redirect stderr to "where stdout is currently going". In this case, that is a file opened in append mode. In other words, the &1 reuses the file descriptor which stdout currently uses.

提交回复
热议问题