当bash / zsh执行以下操作时会发生什么:
~ » /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
为什么我的使用时间有所不同,为什么zsh试图执行-l
??
奇怪的是,zsh说
~ » which time
time: shell reserved word
虽然bash没有:
~ » 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
答案 0 :(得分:9)
time
内置于zsh和bash中。但是,which
仅内置于zsh。在bash中,当你使用which
时,它运行/usr/bin/which
,它不知道shell内置函数。
所以在bash中,你应该使用:
$ type time
time is a shell keyword
time -l ...
不起作用的原因是time
语法不包含-l
标记。
在这两种情况下,说time
是内置函数并不正确。将其称为“保留字”或“shell关键字”更准确,因为它适用于整个管道;它不能作为函数或外部命令实现。从这个意义上说,它类似于if
和while
等其他句法元素。
答案 1 :(得分:3)
了解命令是否为bash内置命令的另一种方法是使用help
内置命令。副作用是获取有关所述命令的信息(以及支持的命令行参数。)
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.
对于非内置命令,help
表示它不知道它。
bash$ help ls
bash: help: no help topics match `ls'. Try `help help' or `man -k ls' or `info ls'.
答案 2 :(得分:1)
时间是zsh中的内置函数。它不是在bash中。如果要使用/ usr / bin / time版本,则需要在使用zsh时提供完整路径。
也可以使用zsh中的“disable -r”命令禁用此行为。