Linux Bash - capture -x debug commands into a file

Using set -x in bash prints shell extended commands to stderr. I would like to redirect them to a file or channel. But not the whole conclusion - only some commands. Something like:

 set -x command.txt ### <-- command.txt param is made up echo $A $B set +x 

This will output the debug command. Txt.

Can this be done?

+8
debugging bash output
source share
1 answer

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 standard output (FD 1):

 hello world! 

Exit to command.txt (FD 5):

 + echo -n world + set +x 
+10
source share

All Articles