为什么`time` 命令不适用于任何选项?

use*_*266 79 command-line bash time-command

我尝试使用time带有-f选项的命令来格式化时间的输出,但出现以下错误:

-f: command not found
Run Code Online (Sandbox Code Playgroud)

然后我试图使用其他选项-a-o等我也得到了同样的错误。甚至time --version不起作用(--version: command not found)。

不要告诉我读那个人,因为我已经读过很多次了......所有这些选项都在那里指定。那么,问题可能出在哪里呢?

Rad*_*anu 114

好吧,即使你不喜欢它,我也会让你多加注意地再读一遍man time。在本EXAMPLES节的末尾,您会发现:

  Users of the bash shell need to use an explicit path in order to run
  the external time command and not the shell builtin variant.  On system
  where time is installed in /usr/bin, the first example would become
       /usr/bin/time wc /etc/hosts
Run Code Online (Sandbox Code Playgroud)

因此,我假设您使用 bash shell,它使用 的内部版本time,作为 shell 关键字提供。您可以使用以下命令进行检查:

type time
Run Code Online (Sandbox Code Playgroud)

输出可能是:

time is a shell keyword
Run Code Online (Sandbox Code Playgroud)

如果是这种情况,那么很明显,要使用真正的 time命令,您必须使用其显式路径:/usr/bin/time.

Further, if you don't want to use anymore the shell keyword time, you can create a permanent alias as follow:

alias time='/usr/bin/time'
Run Code Online (Sandbox Code Playgroud)

This will overwrite the shell keyword time because the command:

type time
Run Code Online (Sandbox Code Playgroud)

will give the following output now:

time is aliased to `/usr/bin/time'
Run Code Online (Sandbox Code Playgroud)

  • 对于 shell 内置命令:{info time} 和 {type time}。对于二进制命令:{which time} 和 {man time} (6认同)
  • 您可以转义命令(即“\time”)以避免将其解释为 shell 内置命令或别名,而不是写出二进制文件的完整路径。 (3认同)

ter*_*don 23

Since, as the other answers explain, time is a shell keyword, the only option available to you is -p:

terdon@oregano ~ $ help time
time: time [-p] pipeline
    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
Run Code Online (Sandbox Code Playgroud)

So, you need to run the time that's in /usr/bin. Here are a few ways to do so:


Rma*_*ano 17

bashzsh壳有自己的内部time命令。你必须使用

/usr/bin/time -f ...
Run Code Online (Sandbox Code Playgroud)

顺便说一句,我发现使用(来自zsh):

~% which  time
time: shell reserved word
Run Code Online (Sandbox Code Playgroud)