bash Shell 执行:时间 vs. /usr/bin/time
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19236448/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Shell execution: time vs. /usr/bin/time
提问by emish
What is going on when bash/zsh does the following:
当 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
Why does it make a difference how I use time, and why is zsh trying to execute -l
??
为什么我如何使用时间会有所不同,为什么 zsh 试图执行-l
?
Curiously, zsh says
奇怪的是,zsh 说
~ ? which time
time: shell reserved word
While bash doesn't:
虽然 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
采纳答案by rici
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.
time
内置于 zsh 和 bash 中。但是,which
仅内置于 zsh 中。在 bash 中,当您使用which
它时,它运行时/usr/bin/which
不知道 shell 内置函数。
So in bash, you should use:
所以在 bash 中,你应该使用:
$ type time
time is a shell keyword
The reason time -l ...
doesn't work is that the time
syntax doesn't include the -l
flag.
原因time -l ...
不起作用是time
语法不包含-l
标志。
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
.
在这两种情况下,说这time
是一个内置函数是不正确的。称其为“保留字”或“shell 关键字”更为准确,因为它适用于整个管道;它不能作为函数或外部命令来实现。从这个意义上说,它类似于其他句法元素,如if
and while
。
回答by Alexis Wilke
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
内置命令。副作用是获取有关所述命令(以及支持的命令行参数)的信息。
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.
对于非内置命令,help
表示它不知道。
bash$ help ls
bash: help: no help topics match `ls'. Try `help help' or `man -k ls' or `info ls'.
回答by Dean Povey
time is a builtin function in zsh. It is not in bash. If you want to use the /usr/bin/time version you need to supply the full path when using zsh.
time 是 zsh 中的内置函数。它不在 bash 中。如果要使用 /usr/bin/time 版本,则需要在使用 zsh 时提供完整路径。
It is also possible to disable this behavior using the "disable -r" command in zsh.
也可以使用 zsh 中的“disable -r”命令禁用此行为。