linux - 如何保存命令的输出及其执行的 'time'?

标签 linux bash shell

我正在尝试执行命令并获取其输出以及运行该命令所需的时间

TIMEFORMAT='real %3R'
VAR1=$( time command );

当我回显 VAR1 时,它显示时间和命令输出。但我似乎无法提取时间子字符串。我尝试过 grep、sed 和 cut。任何对此的提示将不胜感激。

提前致谢。

最佳答案

您得到的输出是什么?您正在使用哪个 bash? 在我的 Mac 上,我得到的时间并不是真正的输出的一部分,而是在之后。

$ TIMEFORMAT='real %3R'
$ time ls|tail -0
real 0.018
$ VAR=$(time ls|tail -0)
real 0.006
$ echo $VAR

我正在使用 GNU bash,版本 3.2.57(1)-release (x86_64-apple-darwin17)

关于linux - 如何保存命令的输出及其执行的 'time'?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53602887/

相关文章:

linux - 如何继续安装Fedora 18 KDE Spin 32位

bash - 如何从文件加载 bash 命令历史

bash - 使用 hexdump 或 xxd 等 shell 实用程序打印十六进制 Unicode 代码点

linux - 作业 : use number of words in one file(through wc) and send it to head as the number of lines

list - wshShell 和 WMIC 批量协同工作?

bash - 如何在 Bash 中获取目录名的最后一部分

linux - clock_gettime 是 UTC 时间还是本地时区?

linux - 为 arm 交叉编译 Qt 的 OpenGL 模块

linux - 如何使用 "Unix built in Command in sed"

linux - 经常在特定时间做某事