带有管道的 Unix“时间”命令不打印时间报告

Sta*_*tan 4 unix command-line csh

在下面的两个 C-shell 别名中,只有第一个打印time命令的已用时间报告。如何使用管道获取别名以打印时间报告?

alias make1 'time make'

alias make2 'time make |& tee make.log'
Run Code Online (Sandbox Code Playgroud)

Arn*_*röm 7

C shell(csh 或 tcsh)内置的time命令不适用于管道。为避免此限制,请改用独立时间命令,该命令通常位于 /usr/bin/time(如果不是,请尝试whereis time找到它)。

time将命令行更改为/usr/bin/time(或/usr/bin/time -p) 或任何时间程序的路径,它应该可以工作。


为什么它不起作用:

C shell(和其他一些 shell 一样)有一个内置时间命令(参见内置手册页),它优先于非内置(独立)时间程序使用:

% which time
time: shell built-in command.
% 
Run Code Online (Sandbox Code Playgroud)

csh手册页(实际上是我系统上的 tcsh)指出:

time [command]
   Executes command (which must be a simple command, not an alias,
   a pipeline, a command list or a parenthesized command list) ...
Run Code Online (Sandbox Code Playgroud)

/usr/bin/time 命令没有这个限制,大多数其他 shell的内置时间命令也没有,例如 bash 或 zsh。