Bash set -x echo redirects as well as commands

bash, io-redirection, pipe

Solution

It isn't possible with `set -x`. Your only option is to view the current command through a `DEBUG` trap.

trap 'printf %s\\n "$BASH_COMMAND" >&2' DEBUG

This won't show the precise arguments as `set -x` will. Combining them should give you the full picture, though it's a lot of debug output.

Problem

I am writing a Bash script where I want all commands to be echoed as they occur. I know that I need to use `set -x` and `set +x` to toggle this behaviour off and on, respectively (SOF post here). However, it doesn't echo everything, namely, I/O redirects. For example, let's say I have this very simple Bash script: ``` set -x ./command1 > output1 ./command2 arg1 arg2 > output2 ``` This will be the output ``` + ./command1 + ./command2 arg1 arg2 ``` Bash is not echoing my stdout redirect to output1 and output2. Why is this? How can I achieve this behaviour? Perhaps is there a `shopt` option that I must set in the script? NOTE: I also noticed that pipes will not print as expected. For example, if I were to use this command: ``` set -x ./command3 | tee output3 ``` I will get this output: ``` + tee output3 + ./command3 ``` How do I make the commands be echoed exactly in the way they are written instead of having the pipe reordered by the script?

Original source

Related problems