Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

Is there a way to send the information displayed by running a bash script with the -x option to a file, while not changing the standard output seen by a user running the script?

This is a debugging feature I would like to implement in a bash script we use that changes frequently.

Much appreciated.

share|improve this question
add comment

3 Answers

set -x doesn't send anything to standard output, so there's no problem there. What it does do is write to standard error, which goes to the console by default.

What you want to do is redirect stdout to another file, like this:

/bin/sh -x /my/script/file 2>/my/log/file
share|improve this answer
add comment

See man tee.

You run it like tee commandname filename and it will display the commands output to stdout and write it also into filename.

share|improve this answer
add comment

The output from -x goes to stderr, not stdin. But even that can be a problem -- plenty of scripts will have functional dependencies on the content of stderr, and its kind of messy to have the debug and stderr streams mixed together in some cases.

Bash versions > 4.1 do offer a different solution: the BASH_XTRACEFD environment variable allows you to specify a file descriptor which will be used to send the debug stream to. This can be a file or pipe or any other unix-y goodness you like.

# Use FD 19 to capture the debug stream caused by "set -x":
exec 19>/tmp/my-script.log
# Tell bash about it  (there's nothing special about 19, its arbitrary)
export BASH_XTRACEFD=19

# turn on the debug stream:
set -x

# run some commands:
cd /etc
find 
echo "Well, that was fun."

# Close the output:
set +x
exec 19>&-

# See what we got:
cat /tmp/my-script.log

With a bit more fiddling, you can do other things -- like doing a 'tee' on the stdout and/or stdin streams, and interleave those with the debug output, so your log is more complete.

The big advantage of this approach over alternatives is that you're not risking changes to the behavior of your script by injecting debug output into stdin or stderr.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.