Time Utility – Is Each `time` a Keyword or `/usr/bin/time`?

timetime-utility

In the following examples, is each time a keyword or /usr/bin/time? Why, or how do you find it out?

can a keyword be a command?

Or does the keyword time apply to an empty command? (I might have asked this somewhere in post or comment, but I can't find it)

Thanks.

$ time time

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

$ time

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

Best Answer

In time time, both are the built-in from bash, none is the external /usr/bin/time command.

This is possible because the time built-in takes a pipeline as its arguments, but time itself is a pipeline, so multiple calls are possible.

If you look at bash source code, you'll even find comments referring to this special case, looking for a call to time following another time or time -p.

You only see the output once because time is implemented by setting a bit flag, so calling it more than once has no effect since it's just setting the same bit on that pipeline...

This, however, calls it in two separate pipelines, so you see the output twice:

$ time { time; }

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

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

You can see results using the external /usr/bin/time by calling it explicitly with the path... Or using \time (the leading \ prevents the shell from using a built-in) or using the command built-in (as in time command time), for example:

$ time command time
Usage: time [-apvV] [-f format] [-o file] [--append] [--verbose]
       [--portability] [--format=format] [--output=file] [--version]
       [--quiet] [--help] command [arg...]

real    0m0.002s
user    0m0.000s
sys 0m0.000s

As you see, the external /usr/bin/time complains when it's called with no arguments... So that's what you're seeing there. (Also, if you use it on an actual command, you'll notice the output format is different from that of the bash built-in.)

Related Question