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

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

 -f: command not found 

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

不要告诉我读这个男人,因为我已经多次这样做了……所有这些选项都在那里指定。 那么,哪里可能是问题?

好吧,即使你不喜欢它,我也会让你再次阅读更多关注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 

所以,我假设你使用bash shell,它使用内部的shell内置版本的time 。 您可以使用以下命令检查:

 type time 

输出可能是:

 time is a shell keyword 

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

此外,如果您不想再使用shell内置time ,可以创建一个永久别名 ,如下所示:

 alias time='/usr/bin/time' 

这将覆盖shell内置time因为命令:

 type time 

现在将给出以下输出:

 time is aliased to `/usr/bin/time' 

因为,正如其他答案所解释的那样, time是一个shell关键字,唯一可用的选项是-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 

因此,您需要运行/usr/bin 。 以下是一些方法:

  • 改为使用time可执行文件:

     /usr/bin/time -f %Uuser ls >/dev/null 
  • 使用\会导致shell忽略别名,内置和关键字,而是在$PATH搜索匹配的可执行文件:

     \time -f %Uuser ls >/dev/null 
  • 使用与上述相同的command builtin

     command time -f %Uuser ls >/dev/null 
  • 使用不同的shell,没有这样的关键字。 例如sh (实际上是在Ubuntu上dash

     sh -c "time -f %Uuser ls >/dev/null" 
  • 使用which ,将搜索你的$PATH (好吧,这个很傻)

     $(which time) -f %Uuser ls >/dev/null 

bashzsh shell有它们的内部time命令。 你必须使用

 /usr/bin/time -f ... 

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

 ~% which time time: shell reserved word