redirect all output in a bash script when using set -x

后端 未结 4 576
青春惊慌失措
青春惊慌失措 2020-12-13 09:05

I have a bash script that has set -x in it. Is it possible to redirect the debug prints of this script and all its output to a file? Ideally I would like to do

4条回答
  •  没有蜡笔的小新
    2020-12-13 09:49

    This is what I've just googled and I remember myself using this some time ago...

    Use exec to redirect both standard output and standard error of all commands in a script:

    #!/bin/bash
    logfile=$$.log
    exec > $logfile 2>&1
    

    For more redirection magic check out Advanced Bash Scripting Guide - I/O Redirection.

    If you also want to see the output and debug on the terminal in addition to in the log file, see redirect COPY of stdout to log file from within bash script itself.

    If you want to handle the destination of the set -x trace output independently of normal STDOUT and STDERR, see bash storing the output of set -x to log file.

提交回复
热议问题