In the Bash Reference Manual,
The use of
timeas a reserved word permits the timing of shell builtins, shell functions, and pipelines. An externaltimecommand cannot time these easily.
Could you explain why the quote says that?
Is this because of the difference between a reserved word and a command, not just limited to the case of
time? For example, how does the bash shell parse or interpret them differently?Or is this only limited to the case of
time?In the following examples,
why does the external
timework on a shell builtin, and a pipeline, while the quote say it "cannot time these easily"?External
timeon a shell builtin:$ /usr/bin/time echo hello hello 0.00user 0.00system 0:00.00elapsed 0%CPU (0avgtext+0avgdata 1676maxresident)k 0inputs+0outputs (0major+78minor)pagefaults 0swapsExternal
timeon a pipeline:$ /usr/bin/time sleep 10 | sleep 5 0.00user 0.00system 0:10.00elapsed 0%CPU (0avgtext+0avgdata 1776maxresident)k 0inputs+0outputs (0major+79minor)pagefaults 0swapsIn the following example, why does the external
timeon a shell function fail? What does its error output mean?$ function mytest () { sleep 10; } $ /usr/bin/time mytest /usr/bin/time: cannot run mytest: No such file or directory Command exited with non-zero status 127 0.00user 0.00system 0:00.03elapsed 0%CPU (0avgtext+0avgdata 1252maxresident)k 32inputs+0outputs (0major+30minor)pagefaults 0swapsIt seems that the quote does not just apply to timing shell builtins, shell functions, and pipelines, but also to timing a group of commands:
$ time { echo hello; sleep 3; echo tim; } hello tim real 0m3.002s user 0m0.000s sys 0m0.000s $ /usr/bin/time { echo hello; sleep 3; echo tim; } bash: syntax error near unexpected token `}'Why does the shell say "bash: syntax error near unexpected token
}" in the case for command/usr/bin/time?