Skip to content
Advertisement

“tee” allows us to redirect output from a command – is there any simple way to output the command string being run itself too?

When looking at log files many times I wonder what options/parameters did I use when running a given command that generated this log file.

Is there any simple way to output the command string being run itself too into stdout to be redirected into the log file at the top of the file?

Advertisement

Answer

You can echo the command, tee it to a file, pipe it to sh, then append the output to the file with tee -a myfile:

echo "ls -la" | tee myfile | sh | tee -a myfile

On stdout, you see just the output of the command, but in myfile, the first line is ls -la and the following lines contain the output of the command.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement