为什么我在shell中输入的时间命令在linux中输出的时间与我在脚本中使用时的输出不同?

l24*_*c4l 3 linux bash time

问题是,当我在shell中使用时间时,我得到如下输出:

1.350u 0.038s 0:01.45 95.1%     0+0k 0+72io 1pf+0w
Run Code Online (Sandbox Code Playgroud)

当我在脚本中使用它时,我得到:

real    0m1.253s
user    0m1.143s
sys     0m0.047s
Run Code Online (Sandbox Code Playgroud)

我的意思是为什么 在开头的shell脚本中我写道:

#!/bin/bash
Run Code Online (Sandbox Code Playgroud)

Mar*_*off 5

Bash有一个内置命令time,你的系统也应该有一个单独的二进制文件/usr/bin/time:

$ 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,
    ...
$ which time
/usr/bin/time

这两个命令产生不同的输出:

$ time echo hi
hi

real    0m0.000s
user    0m0.000s
sys 0m0.000s
$ /usr/bin/time echo hi
hi
0.00user 0.00system 0:00.00elapsed 100%CPU (0avgtext+0avgdata 0maxresident)k
0inputs+0outputs (0major+199minor)pagefaults 0swaps

  • 在Bash中执行`type -a time`:它将显示Bash知道的所有可执行文件,名为`time`.在我的例子中,它说`time是一个shell关键字',`time是/ usr/bin/time`.我建议人们更喜欢在Bash中使用`type`而不是`which`.(我们曾经多年前遇到过问题,因为```使用了C shell,而不是Bash,而且由于我们内部不支持C shell,因此它的`$ PATH`往往是系统默认的并且不正确.) (3认同)