use*_*266 79 command-line bash time-command
我尝试使用time带有-f选项的命令来格式化时间的输出,但出现以下错误:
-f: command not found
Run Code Online (Sandbox Code Playgroud)
然后我试图使用其他选项-a,-o等我也得到了同样的错误。甚至time --version不起作用(--version: command not found)。
不要告诉我读那个人,因为我已经读过很多次了......所有这些选项都在那里指定。那么,问题可能出在哪里呢?
Rad*_*anu 114
好吧,即使你不喜欢它,我也会让你多加注意地再读一遍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
Run Code Online (Sandbox Code Playgroud)
因此,我假设您使用 bash shell,它使用 的内部版本time,作为 shell 关键字提供。您可以使用以下命令进行检查:
type time
Run Code Online (Sandbox Code Playgroud)
输出可能是:
time is a shell keyword
Run Code Online (Sandbox Code Playgroud)
如果是这种情况,那么很明显,要使用真正的 time命令,您必须使用其显式路径:/usr/bin/time.
Further, if you don't want to use anymore the shell keyword time, you can create a permanent alias as follow:
alias time='/usr/bin/time'
Run Code Online (Sandbox Code Playgroud)
This will overwrite the shell keyword time because the command:
type time
Run Code Online (Sandbox Code Playgroud)
will give the following output now:
time is aliased to `/usr/bin/time'
Run Code Online (Sandbox Code Playgroud)
ter*_*don 23
Since, as the other answers explain, time is a shell keyword, the only option available to you is -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
Run Code Online (Sandbox Code Playgroud)
So, you need to run the time that's in /usr/bin. Here are a few ways to do so:
Use the time executable instead:
/usr/bin/time -f %Uuser ls >/dev/null
Run Code Online (Sandbox Code Playgroud)Use \ which causes your shell to ignore aliases and keywords and instead search your $PATH for a matching executable:
\time -f %Uuser ls >/dev/null
Run Code Online (Sandbox Code Playgroud)Use the command builtin which has a similar effect:
command time -f %Uuser ls >/dev/null
Run Code Online (Sandbox Code Playgroud)使用不同的 shell,一个没有这样的关键字的 shell。例如sh(实际上是dash在 Ubuntu 上):
sh -c "time -f %Uuser ls >/dev/null"
Run Code Online (Sandbox Code Playgroud)使用which,它将搜索您的$PATH(好吧,这个很傻):
$(which time) -f %Uuser ls >/dev/null
Run Code Online (Sandbox Code Playgroud)Rma*_*ano 17
在bash与zsh壳有自己的内部time命令。你必须使用
/usr/bin/time -f ...
Run Code Online (Sandbox Code Playgroud)
顺便说一句,我发现使用(来自zsh):
~% which time
time: shell reserved word
Run Code Online (Sandbox Code Playgroud)