Capture -x debug commands into a file in Bash

后端 未结 1 391
迷失自我
迷失自我 2020-12-14 20:07

Using set -x in bash prints the shell-expanded commands to stderr. I would like to redirect these to a file or pipe. But not the whole output - only some comman

相关标签:
1条回答
  • 2020-12-14 20:58

    With bash 4.1 or later:

    #!/bin/bash
    
    exec 5> command.txt
    BASH_XTRACEFD="5"
    
    echo -n "hello "
    
    set -x
    echo -n world
    set +x
    
    echo "!"
    

    Output to stdout (FD 1):

    hello world!
    

    Output to command.txt (FD 5):

    + echo -n world
    + set +x
    
    0 讨论(0)
提交回复
热议问题