In tcsh, there's the variable time:

   The time shell variable can be set to execute the time builtin command
   after the completion of any process that takes more than a given number
   of CPU seconds.

How do I do this in bash?


I don't think you can achieve exactly the same effect without modifying the bash source. But you can get close, hopefully close enough for you.

You can combine bash's hacky precommand hook and the SECONDS variable to show the wall clock time in a non-intrusive way. Here's a simple implementation due to Ville Laurikari. The functions timer_start and timer_stop are executed immediately before starting a command and immediately before displaying the next prompt.

function timer_start {
function timer_stop {
  timer_show=$(($SECONDS - $timer))
  unset timer
trap 'timer_start' DEBUG
PS1='[last: ${timer_show}s][\w]$ '

To get the full time information for every command, here's a way due to Dennis Williamson:

bind '"\C-j": "\C-atime {\C-e;}\C-m"'

When you press Ctrl+J instead of Enter to start a command, you'll get time information. Rebinding Enter (i.e. Ctrl+M) is not recommended because the modified command will sometimes be syntactically incorrect.

See How can the last command's wall time be put in the Bash prompt? and Automatically timing every executed command and show in Bash prompt? on Stack Overflow for other methods (note however that most only give the elapsed real time, not the CPU time).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.