Linux – call `time` with format option

linuxtime

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)

Best Answer

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.

Related Question