0

How to pass each command (typed in the shell) through time command?

Reason of the question: need to measure execution time of each command.

Example (wanted behavior):

$ sleep 1
real    0m1.047s
user    0m0.015s
sys     0m0.000s
pmor
  • 5,392
  • 4
  • 17
  • 36
  • 1
    Consider a DEBUG trap. – Charles Duffy Apr 02 '20 at 13:59
  • That said, insofar as you're trying to instrument an interactive shell rather than a script, this might be more a question for our sister site [unix.se], as Stack Overflow's focus is strictly on software development. – Charles Duffy Apr 02 '20 at 14:03
  • @Charles Duffy, thanks! But after "trap "time" DEBUG" on "$ sleep 1" it gives: "real 0m0.000s", i.e. it executes "time" and then "sleep 1". The same result can be achieved with using PROMPT_COMMAND. One need to automatically convert "sleep 1" to "time sleep 1". – pmor Apr 02 '20 at 14:33
  • I didn't say `trap "time" DEBUG`. DEBUG traps let you do a lot more than that; you can cancel the original command and replace it with your own. The *how* of doing that... well, if I were answering it right now, this would be an answer, not a comment. – Charles Duffy Apr 02 '20 at 14:45
  • Take a look at the [unix.se] question [modify all bash commands through a program before executing them](https://unix.stackexchange.com/questions/250713/modify-all-bash-commands-through-a-program-before-executing-them). – Charles Duffy Apr 02 '20 at 14:47
  • @Charles Duffy, thanks for the information and for the link. On the Unix & Linux site @Arkadiusz Drabczyk has suggested solutions posted here: https://stackoverflow.com/questions/1176386/automatically-timing-every-executed-command-and-show-in-bash-prompt. One solution is based on `DEBUG` trap and one on `bind` (quite original, isn't it?). For the moment I use the 1st one (with small adaptations for my needs). – pmor Apr 02 '20 at 17:30
  • Do you have need for further/separate answers here? Otherwise, I'll close this as a duplicate of the other SO question you just posted -- which will still leave it up and available for search engines to index, but with a pointer to the other question and its answers in the header. – Charles Duffy Apr 02 '20 at 17:33
  • @Charles Duffy, yes (if possible). Both prev. solutions are failed on `sleep 1 && sleep 3`: solution based on `bind` gives `0m1.037s` (it measured execution of the `sleep 1` only), solution based on `DEBUG` gives `3` (it measured execution of the `sleep 3` only). Meaning one cannot use these solutions in case of combining commands via && operator. If you know any solution for this, please let me know. (It should be a way to take ALL the command chain and apply `time` on it -- maybe via `()` parentheses). – pmor Apr 02 '20 at 17:46
  • Parenthesis have side effects you don't want -- they move content into a subshell, so the effects of any variable assignments, directory changes, etc. are undone when it exits. Using `{` and `}` for grouping, with a literal newline added before the final `}`, is more plausible. – Charles Duffy Apr 02 '20 at 17:50
  • 1
    @Charles Duffy: UPD: I've found a complete solution here: https://stackoverflow.com/a/34812608/9881330. It handles combination of commands (via both `&&` and `;` operators) and offers many more other useful features! So, now please close this as a duplicate and (if possible) provide the link (above) as a complete solution. Thanks! – pmor Apr 02 '20 at 17:56
  • I can only link to another question, not a specific answer within it -- so I'm upvoting your comment linking to the one answer to make it more visible; I'm assuming you already gave the answer an upvote yourself. :) – Charles Duffy Apr 02 '20 at 18:06

0 Answers0