Bash 获取脚本执行时间

Bash 获取脚本执行时间,bash,shell,awk,sed,sh,Bash,Shell,Awk,Sed,Sh,我正试图使用下面的bash脚本找出脚本执行时间 start=$(date +%s.%N) # run time # code to execute script end=$(date +%s.%N) # end time printf '\n"total_time":' "$((start-end))" 但是从printf语句中获取以下错误 syntax error: invalid arithmetic operator (error token is ".064560120") 我还想在

我正试图使用下面的bash脚本找出脚本执行时间

start=$(date +%s.%N) # run time
# code to execute script
end=$(date +%s.%N) # end time
printf '\n"total_time":' "$((start-end))"
但是从printf语句中获取以下错误

syntax error: invalid arithmetic operator (error token is ".064560120")

我还想在输出中包含单词s或ms

Bash不做浮点运算。您必须使用其他shell,或调用外部程序,如bc

%printf上的s将打印bc的原始输出

例如,在zsh中,它直接在外壳中工作:

$ zsh -c 'a=$(date +%s.%N); sleep .4; b=$(date +%s.%N); echo "runtime: $(( b - a ))" seconds'
runtime: 0.40290713310241699 seconds

另请参见:在Unix.SE上。

您知道时间吗?你能告诉我printf在这种情况下会是什么样子吗?printf'\n运行时:$b-$a | bc-l给出了一个error@all_techie,将对bc的调用包装在命令替换$。。。捕获输出。示例已编辑。
$ zsh -c 'a=$(date +%s.%N); sleep .4; b=$(date +%s.%N); echo "runtime: $(( b - a ))" seconds'
runtime: 0.40290713310241699 seconds