Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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?

share|improve this question
add comment

1 Answer

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 {
  timer=${timer:-$SECONDS}
}
function timer_stop {
  timer_show=$(($SECONDS - $timer))
  unset timer
}
trap 'timer_start' DEBUG
PROMPT_COMMAND=timer_stop
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).

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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