我正在尝试获取运行命令所需的时间,并以特定格式输出:
time -f "%E" ls -l
这与手册页中的示例类似(并且在线手册页)。但是当我运行此命令时,我得到:
-f: command not found
看起来好像时间命令没有将 -f 读取为参数,而是将其读取为我尝试运行的命令。
如何获取特定格式的命令的执行时间?
答案1
这是因为time
它是一个 bash 内置命令 - 并且该内置命令不支持您尝试使用的选项。
尝试一下,使用时间的完整路径跳过内置的并使用真实的路径:
/usr/bin/time -f "%E" ls -l
答案2
不幸的是,time
既是 bash 关键字,又是 中的程序/usr/bin
。如果您指定完整路径,例如time
:
/usr/bin/time -f "%E" ls -l
您将获得所期望的输出。
答案3
改变 bash 内置命令的格式时间使用时间格式多变的
TIMEFORMAT
The value of this parameter is used as a format string specifying how the timing information for pipelines prefixed with the time reserved word should be displayed. The % character introduces an escape sequence that is expanded to a time value or
other information. The escape sequences and their meanings are as follows; the braces denote optional portions.
%% A literal %.
%[p][l]R The elapsed time in seconds.
%[p][l]U The number of CPU seconds spent in user mode.
%[p][l]S The number of CPU seconds spent in system mode.
%P The CPU percentage, computed as (%U + %S) / %R.
The optional p is a digit specifying the precision, the number of fractional digits after a decimal point. A value of 0 causes no decimal point or fraction to be output. At most three places after the decimal point may be specified; values of p
greater than 3 are changed to 3. If p is not specified, the value 3 is used.
The optional l specifies a longer format, including minutes, of the form MMmSS.FFs. The value of p determines whether or not the fraction is included.
If this variable is not set, bash acts as if it had the value $'\nreal\t%3lR\nuser\t%3lU\nsys\t%3lS'. If the value is null, no timing information is displayed. A trailing newline is added when the format string is displayed.