Shell执行:时间与/usr/bin/time [英] Shell execution: time vs. /usr/bin/time
问题描述
bash/zsh执行以下操作时会发生什么:
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
为什么它会改变我的时间使用方式,为什么zsh会尝试执行-l
?
Why does it make a difference how I use time, and why is zsh trying to execute -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
推荐答案
time
内置在zsh和bash中.但是,which
仅内置于zsh.在bash中,当您使用which
时,它将运行/usr/bin/which
,而对外壳程序内置组件一无所知.
time
is builtin in both zsh and bash. However, which
is only built-in to zsh. In bash, when you use which
it runs /usr/bin/which
which has no idea about shell built-ins.
因此在bash中,您应该使用:
So in bash, you should use:
$ type time
time is a shell keyword
time -l ...
不起作用的原因是time
语法不包含-l
标志.
The reason time -l ...
doesn't work is that the time
syntax doesn't include the -l
flag.
在两种情况下,都说time
是内置函数并不正确.称其为保留字"或"shell关键字"更为准确,因为它适用于整个管道.它不能实现为功能或外部命令.从这个意义上讲,它类似于if
和while
之类的其他语法元素.
In both cases, it's not really correct to say that time
is a built-in function. Calling it a "reserved word" or "shell keyword" is more accurate, because it applies to an entire pipeline; it cannot be implemented as a function or external command. In that sense, it is similar to other syntactic elements like if
and while
.
这篇关于Shell执行:时间与/usr/bin/time的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!