5

I want to measure execution time of commands and get (to me) strange behavior of the time command.

I call time like it is mentioned in the examples of the manpages with the -f or --format option and get a command not found error. Seems like it thinks -f is the command to measure the execution time of...

user@laptop:~$ time -f '%e' sleep 1
-f: Befehl nicht gefunden.

Then I call the same line again, just that I specify the path to time. Result changes, works as intended (wtf?):

user@laptop:~$ /usr/bin/time -f '%e' sleep 1
1.00

Now I'm confused... even which can't help me:

user@pc:~$ which time
/usr/bin/time

And... if I don't use options it works fine, even without full path:

user@laptop:~$ time sleep 1

real    0m1.003s
user    0m0.001s
sys 0m0.002s

How do I use this command correctly if I want to specify options? Using the full path seems to work, but looks more like an unreliable side effect of something buggy (or something I don't understand)?

Edit: I'm using Ubuntu 14.04 LTS (trusty)

5

Bash has a built in command called time. So if you just type time it will use the shell built-in which has no options like -f. But man time (which talks about -f) will give the manpage of the /usr/bin/time program. So if you want to use options described in the manpage you should ensure you call the program, not the shell built-in. One way achieving this is using the full path.

(IMHO, this is really ugly in bash)

For more info, see the links below the question.

  • Actually time isn't a shell builtin it's a reserved word. – user591 Mar 1 '16 at 6:16
  • Do you have a reference for that? unix.stackexchange.com/questions/183745/… Similar to a builtin, a keyword is hard-coded into Bash, but unlike a builtin, a keyword is not in itself a command, but a subunit of a command construct. So are you saying time is a subunit of a command construct ? – Boris Däppen Mar 1 '16 at 12:52
  • It is in POSIX, The reason: time "needs" to be transparent to pipes and redirections. The "needs": for practical common uses and historical reasons. EDIT: POSIX uses "times", but the reason is the same. – Giacomo Catenazzi Mar 1 '16 at 14:11
0

Just thought I would add a way to show the info for Boris's answer.

As was just puzzled by time again.

In bash type is your friend. I should really remember to use it more. Old csh habits die hard.

which -a time  
/usr/bin/time

type time 
time is a shell keyword

type times  
times is a shell builtin

type nslookup  
nslookup is /usr/bin/nslookup

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.