Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shell execution: time vs. /usr/bin/time

What is going on when bash/zsh does the following:

~ » /usr/bin/time -l sleep 1                                                                                                              
    1.00 real         0.00 user         0.00 sys
516096  maximum resident set size
     0  average shared memory size
     0  average unshared data size
     0  average unshared stack size
   145  page reclaims
     0  page faults
     0  swaps
     0  block input operations
     0  block output operations
     0  messages sent
     0  messages received
     0  signals received
     0  voluntary context switches
     2  involuntary context switches
------------------------------------------------------------
~ » time -l sleep 1                                                                                                                       
zsh: command not found: -l
-l sleep 1  0.00s user 0.00s system 52% cpu 0.001 total
------------------------------------------------------------
~ » /usr/bin/time foo                                                                                                                     
foo: No such file or directory
        0.00 real         0.00 user         0.00 sys
------------------------------------------------------------
~ » time foo                                                                                                                              
zsh: command not found: foo
foo  0.00s user 0.00s system 52% cpu 0.001 total

Why does it make a difference how I use time, and why is zsh trying to execute -l??

Curiously, zsh says

~ » which time                                                                                                                            
time: shell reserved word

While bash doesn't:

~ » bash                                                                                                                                  
bash-3.2$ which time
/usr/bin/time
bash-3.2$ time foo
bash: foo: command not found

real    0m0.006s
user    0m0.000s
sys 0m0.003s
bash-3.2$ /usr/bin/time foo
foo: No such file or directory
        0.00 real         0.00 user         0.00 sys
bash-3.2$ time -l sleep 1
bash: -l: command not found

real    0m0.001s
user    0m0.000s
sys 0m0.001s
bash-3.2$ /usr/bin/time -l sleep 1
        1.00 real         0.00 user         0.00 sys
    516096  maximum resident set size
         0  average shared memory size
         0  average unshared data size
         0  average unshared stack size
       144  page reclaims
         0  page faults
         0  swaps
         0  block input operations
         1  block output operations
         0  messages sent
         0  messages received
         0  signals received
         2  voluntary context switches
         2  involuntary context switches
like image 412
emish Avatar asked Oct 07 '13 23:10

emish


People also ask

What is usr bin time?

/usr/bin/time is a useful command (not to confuse with “time”) that can measure “real,user,sys”, but also “maximum resident set size, page faults, swaps” and other very useful system info for a specific program. In GNU stack based unix, just do /usr/bin/time.

What does Time Command do in Linux?

time command in Linux is used to execute a command and prints a summary of real-time, user CPU time and system CPU time spent by executing a command when it terminates.


1 Answers

Another way to know whether a command is a bash builtin command is to use the help builtin command. The side effect is to get the information about said command (and supported command line arguments.)

bash$ help time
Report time consumed by pipeline's execution.

Execute PIPELINE and print a summary of the real time, user CPU time,
and system CPU time spent executing PIPELINE when it terminates.

Options:
  -p    print the timing summary in the portable Posix format

The value of the TIMEFORMAT variable is used as the output format.

Exit Status:
The return status is the return status of PIPELINE.

For non-builtin commands, help says it does not know about it.

bash$ help ls
bash: help: no help topics match `ls'.  Try `help help' or `man -k ls' or `info ls'.
like image 152
Alexis Wilke Avatar answered Sep 23 '22 05:09

Alexis Wilke