0

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
4

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.)

  • Thanks. Is time timing an empty command? – Tim May 17 '18 at 20:19
1

If you see only one output in the first example, this is a shell bug.

In shells that re-implement the ksh concept where time is a keyword, time without arguments print the timing for the whole pipeline that in this special case is just the single time keyword.

If you call time time, the first time prints the timing for the whole pipeline and the second time prints the timing for itself only.

So the time keyword alone indeed is a keyword before an empty command.

0

Could be both depending on your setup.
You can see what it is on your system with this command:

$ type time
time is a shell keyword

In your example both time in time time will point to the same command.

  • builtin and keyword are two different concepts and have different parsing rules. For example, builtins can be quoted but keywords can't: "echo" foo will run the echo builtin but "time" command will run the time external command instead of the time keyword. Also note that POSIX requires time to be an external command, not a builtin nor a keyword. – nxnev May 18 '18 at 3:20
  • @nxnev well: "In the KornShell, time is a shell reserved word that can be used to time an entire pipeline, rather than just a simple command. The POSIX definition has been worded to allow this implementation." – muru May 18 '18 at 4:25
  • I remember the original question being quite different and my answer making more sense at that point. Either way, I edited my answer. – Mikael Kjær May 18 '18 at 16:37

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.