Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a script that issues a lot of commands. I wish to save the output of this script (Errors and stdout) to a file, but I also want the actual commands that were executed to be saved to the file as well (as displayed by bash -x)

I've tried:

bash -x > log.txt

to no avail

Any help is appreciated. Thanks

share|improve this question
Can you get some idea from ? – Pothi Oct 29 '13 at 2:35
have you tried piping to tee? – dyasny Oct 29 '13 at 3:13

I had tested the following its giving what you are looking for, The command below will log stdout/stderror to the file output.txt

bash -x &> output.txt


share|improve this answer

I believe your problem is that the output from "bash -x" is sent to stderr while your script's output is being sent to stdout.

When you executed:

   bash -x > output.txt

...the normal output from your script was sent to the redirected file and the script commands (from the -x) continued to be displayed on your terminal screen.

In order to send stderr and stdout to the same stream, you need to duplicate one atop the other. This is done using shell file descriptor dupe syntax "e.g. 2>&1"), like so:

   bash -x > output.txt 2>&1

This syntax duplicates file descriptor #2 (stderr) over the top of file descriptor #1 (stdout)... which results in both streams getting redirected to your output file.

share|improve this answer

Your Answer


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.