#!/bin/bash
start=$(date +%s)
yum install httpd -y

end=$(date +%s)
different=$((end - start))
echo $different
echo time taken to execute commands is $difference seconds.

本例子是在测试安装http所需要的时间

如果要查看一个命令花费的时间也可以使用time

[root@iZ2ze20j4zsv1q8o9py23wZ ~]# time find . -name 'a.sh'
./a.sh

real    0m0.013s
user    0m0.000s
sys 0m0.002s

其中,
real表示time后的命令实际使用时间(real time),即是从command命令开始执行到运行终止的消耗的总时间。

user表示time后的命令执行完成花费的用户CPU时间,即命令在用户态中执行时间总和。

sys表示time后的命令执行完成花费的系统CPU时间,即命令在核心态中执行时间总和。

备注,通常来说real>user+sys,比说说命令执行会等待IO,等待IO时并没有实际消耗CPU的时间。