为什么 Ubuntu 的 `time` 不遵循标准的 POSIX 格式,我该如何更改它?

pvo*_*orb -1 command-line posix-compliant

如果您在 Ubuntu 18.04 中测量时间,结果会显示在一行中:

$ time sleep 1
sleep 1  0.00s user 0.00s system 0% cpu 1.003 total
Run Code Online (Sandbox Code Playgroud)

这不符合 POSIX 标准,如time的手册页中所述:

-p, --portability
       Use the following format string, for conformance with POSIX standard 1003.2:
                 real %e
                 user %U
                 sys %S
Run Code Online (Sandbox Code Playgroud)

现在我的问题是:

为什么 Ubuntu 使用自己的格式来显示资源使用情况?

另外,我如何改变行为?使用-p我上面引用的手册中的选项,我只会收到以下错误:

$ time -p sleep 1           
zsh: command not found: -p
-p sleep 1  0.00s user 0.00s system 73% cpu 0.002 total
Run Code Online (Sandbox Code Playgroud)

gui*_*erc 6

你的错误是一个巨大的线索 -

zsh:找不到命令:-p

您使用的是 zsh 而不是 BASH 或 DASH。

time 的手册页属于外部 /usr/bin/time 程序:bash 和 zsh 提供了自己的内置时间 shell(恰好有一个等效的 -p 开关)

更正@steeldriver 的评论

我使用默认的 bash 在我的标准 Ubuntu 上得到了不同的结果 - 或者你无疑想要/期望的结果。

guiverc@d960-ubu2:~$  time -p sleep 1
real 1.09
user 0.00
sys 0.00
Run Code Online (Sandbox Code Playgroud)

  • `time` 的手册页属于外部 `/usr/bin/time` 程序:`bash` 和 `zsh` 提供了自己的 `time` shell 内置(恰好有一个等效的 `-p` 开关),但说手册页是“用于 bash/dash/sh”并不正确 (6认同)