14

It may be a newbie's question, but I don't understand how this is configured and why the output format of time command is different in these two cases:

if used via time, the output is three rows with basic info

$ time sleep 1

real    0m1.003s
user    0m0.000s
sys     0m0.000s

then I can check out which binary is used

$ which time
/usr/bin/time

and call it directly to get output in a completely different format, with much more info

$ /usr/bin/time sleep 1
0.00user 0.00system 0:01.00elapsed 0%CPU (0avgtext+0avgdata 2000maxresident)k
0inputs+0outputs (0major+77minor)pagefaults 0swaps

there are no aliases related to time

$ alias | grep time
$ 

I'm running Ubuntu 16.04.

muru
  • 193,181
  • 53
  • 473
  • 722
xaxa
  • 439
  • 3
  • 14

2 Answers2

23

The first one is the bash's own builtin keyword time (compiled with bash), and the second one is the external executable time (/usr/bin/time, comes with the time package).

Also, which can't show the shell's builtin commands or keywords as it just searches through PATH, you need to use type for that. Being a shell builtin itself, type can additionally check for shell's internal entities (and also PATH), so you can spot the difference by:

type -a time

Here:

$ type -a time
time is a shell keyword
time is /usr/bin/time

The first one will be executed if you just use time. You can also get which is being executed by just using type (without -a):

type time

The -a tells type to search in shell's internal entities and also in PATH i.e. search in all possible sources.

If for some reason you need the external one, use any one of:

\time
"time"
'time'
command time
heemayl
  • 90,425
  • 20
  • 200
  • 267
  • ah ,I see, so `which` does not consider builtins? And I should always check with `type -a` first. Actually, it seems `type` is superior to `which` because it outputs location `/usr/bin/time` as well – xaxa Aug 31 '16 at 20:10
  • @xaxa Yeah, check my edits. – heemayl Aug 31 '16 at 20:11
  • thanks for a detailed answer! What is `which` useful for then? – xaxa Aug 31 '16 at 20:12
  • @xaxa When you just want to search through `PATH`. Note that, the same can be achieved by `type` using `-P` e.g. `type -P time`. – heemayl Aug 31 '16 at 20:18
  • 1
    @xaxa `type` is indeed superior --- if you're using bash. Another shell may not have either the `type` bulitin, or the `time` builtin. – jpaugh Aug 31 '16 at 20:25
  • @jpaugh good point – xaxa Aug 31 '16 at 20:27
  • 2
    @jpaugh Not quite right. POSIX allows `time` to be external only but not `type`, `type` is available in all POSIX compliant shells. – heemayl Aug 31 '16 at 20:35
  • @heemayl Non-POSIX shells may not have `type` though. On `csh` and `tcsh`, not only `type` doesn't exist, but `which` is different and tells you about built-in commands. – Random832 Aug 31 '16 at 20:49
  • @heemayl Your post was especially informative, as I've never used `type` before. Since I'll never go near a non-POSIX shell, it's good news that POSIX specifies it. – jpaugh Aug 31 '16 at 21:04
4

Another difference between the builtin and external utilities is, that Bash's builtin time will time complete pipelines or calls to shell functions (apparently even loops, but the manual doesn't seem to promise that). The external time cannot, since being outside the shell, doesn't know about the surrounding code.

bash$ time echo blah | sleep 3
real    0m3.002s
...
bash$ /usr/bin/time echo blah | sleep 3
0.00user 0.00system 0:00.00elapsed ?%CPU 
...
bash$ time for x in 1 2 3 ; do sleep 1 ; done
real    0m3.006s
...

While time is specified in the standard, it's left unspecified how it should act in a pipeline, so a more powerful internal implementation like this is possible.

ilkkachu
  • 1,716
  • 9
  • 14