Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have the following code in my bashrc to get the execution time of the last command from http://stackoverflow.com/a/1862762

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]$ '

but when I run echo $_ it shows "timer_start" instead of the last commands parameter.

How can I change this to preserve the $_ variable?

share|improve this question
add comment

1 Answer 1

up vote 1 down vote accepted

You could do:

trap '__=$_; timer_start; : "$__"' DEBUG
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.