set -x
or set -v
prints every executed command.
How do I get the command printed with the time when the command started executing?
set -x
or set -v
prints every executed command.
How do I get the command printed with the time when the command started executing?
In Bash, you can use
PS4
- which is used byset -x
to prefix tracing output.in Bash, execution of code after each command can be achieved using the "trap" builtin and the "DEBUG" level.
in bash do: help trap or refer to bash manual page, and look for the trap section
example:
This will execute the command "date" juste after each command.
Of course you can format the timestamp as you want, refer to the "date" manual page.
If you want to remove the newline, so that timestamp appears in front of next command, you can do:
And to terminate this:
You can combine with "set -x" / "set +x"
Not saying this is a perfect solution by any means, but I think it basically accomplishes what you need. Just wrap the main script in a function and have a separate function to call the time. I'm sure this could be greatly improved upon to get the exact desired result, but here's what I came up with:
The above script outputs the following: