Redirect all output to file

This question already has an answer here:

  • How can I redirect and append both stdout and stderr to a file with Bash? 6 answers

  • That part is written to stderr, use 2> to redirect it. For example:

    foo > stdout.txt 2> stderr.txt
    

    or if you want in same file:

    foo > allout.txt 2>&1
    

    Note: this works in (ba)sh, check your shell for proper syntax


    All POSIX operating systems have 3 streams: stdin, stdout, and stderr. stdin is the input, which can accept the stdout or stderr. stdout is the primary output, which is redirected with > , >> , or | . stderr is the error output, which is handled separately so that any exceptions do not get passed to a command or written to a file that it might break; normally, this is sent to a log of some kind, or dumped directly, even when the stdout is redirected. To redirect both to the same place, use:

    command &> /some/file

    EDIT : thanks to Zack for pointing out that the above solution is not portable--use instead:

    *command* > file 2>&1 
    

    If you want to silence the error, do:

    *command* 2> /dev/null
    

    To get the output on the console AND in a file file.txt for example.

    make 2>&1 | tee file.txt
    

    Note: & (in 2>&1 ) specifies that 1 is not a file name but a file descriptor.

    链接地址: http://www.djcxy.com/p/24144.html

    上一篇: 检查传递给Bash脚本的参数的数量

    下一篇: 将所有输出重定向到文件