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 script.sh > log.txt
to no avail
Any help is appreciated. Thanks
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 script.sh &> output.txt
Sandeep
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:
...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:
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.