Quantcast
Viewing all articles
Browse latest Browse all 4

Answer by David Foerster for Direct output from a command to a file including the original command, AND print in terminal

You can use the debugging function of the shell together with tee:

( set -x; command1 args...; command2 args ) 2>&1 | tee output.log
  • ( ... ) starts a sub-shell which allows you to “collect” the output streams of all commands executed within the sub-shell. It also contains the effect of the set command below to this sub-shell.

  • set -x enables the x shell option which prints all commands that the shell runs to the standard error stream before running them.

  • 2>&1 redirects stream 2 (standard error) to stream 1 (standard output).

  • | redirects the the standard output stream of the left command to the standard input stream of the right command.

  • tee FILE copies the standard input stream to the file FILE and to standard output.

If your command sequence is already in a script file it would make more sense to run it like this:

bash -x /path/to/script args... 2>&1 | tee output.log

Viewing all articles
Browse latest Browse all 4

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>