164

I am writing a script in bash to calculate the time elapsed for the execution of my commands, consider:

STARTTIME=$(date +%s)
#command block that takes time to complete...
#........
ENDTIME=$(date +%s)
echo "It takes $($ENDTIME - $STARTTIME) seconds to complete this task..."

I guess my logic is correct however I end up with the following print out:

"It takes seconds to complete this task..."

Anything wrong with my string evaluation?

I believe bash variables are untyped, I would love if there is a "string to integer" method in bash nevertheless.

1

12 Answers 12

229

I find it very clean to use the internal variable "$SECONDS"

SECONDS=0 ; sleep 10 ; echo $SECONDS

4
  • 4
    $SECONDS does indeed work for /bin/bash. It does not work for the /bin/dash, the default shell in Debian and Ubuntu. Commented Jun 16, 2016 at 19:38
  • 3
    Downside to this solution is that it only measures whole seconds, i.e. not usable if you need sub-second precision. Commented Oct 27, 2017 at 16:01
  • @Czechnology yes, if you use sleep 0.5 in above, the result is sometimes 0, sometimes 1 (at least by Bash 5.0.3).
    – jarno
    Commented Jun 5, 2020 at 9:42
  • 1
    start=`date +%s%N` ; end=`date +%s%N` ; echo $(( ($end - $start) )) Gives you nanoseconds start=`date +%s%N` ; end=`date +%s%N` ; echo $(( ($end - $start) / 1000 )) Gives you microseconds start=`date +%s%N` ; end=`date +%s%N` ; echo $(( ($end - $start) / 1000000 )) Gives you milliseconds
    – Lon Kaut
    Commented Jan 20, 2022 at 16:15
96

Either $(()) or $[] will work for computing the result of an arithmetic operation. You're using $() which is simply taking the string and evaluating it as a command. It's a bit of a subtle distinction.

As tink pointed out in the comments on this answer, $[] is deprecated, and $(()) should be favored.

1
  • 7
    You may want to swap those two around, as bash 4.x man-page states that the $[] is deprecated and will be removed in future versions.
    – tink
    Commented Jun 4, 2013 at 1:24
65

You are trying to execute the number in the ENDTIME as a command. You should also see an error like 1370306857: command not found. Instead use the arithmetic expansion:

echo "It takes $((ENDTIME - STARTTIME)) seconds to complete this task..."

You could also save the commands in a separate script, commands.sh, and use time command:

time commands.sh
0
36

You can use Bash's time keyword here with an appropriate format string

TIMEFORMAT='It takes %R seconds to complete this task...'
time {
    #command block that takes time to complete...
    #........
 }

Here's what the reference says about 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.

2
  • If you need to include backslash-escaped characters in the TIMEFORMAT variable you have to use a $ at the beginning. For example, TIMEFORMAT='\nreal: %3R' won't work, while TIMEFORMAT=$'\nreal: %3R' will. The answer of course implies that, just wanted to make it more explicit.
    – user3712541
    Commented Dec 5, 2020 at 22:44
  • @blaze9 Sure, it's called ANSI-C quotes, and its use is not limited to TIMEFORMAT: you can use it each time you want to include a backslash escape in any string. E.g., a=$'hello\nworld' and then echo "$a". Commented Dec 6, 2020 at 7:43
19

For larger numbers we may want to print in a more readable format. The example below does same as other but also prints in "human" format:

secs_to_human() {
    if [[ -z ${1} || ${1} -lt 60 ]] ;then
        min=0 ; secs="${1}"
    else
        time_mins=$(echo "scale=2; ${1}/60" | bc)
        min=$(echo ${time_mins} | cut -d'.' -f1)
        secs="0.$(echo ${time_mins} | cut -d'.' -f2)"
        secs=$(echo ${secs}*60|bc|awk '{print int($1+0.5)}')
    fi
    echo "Time Elapsed : ${min} minutes and ${secs} seconds."
}

Simple testing:

secs_to_human "300"
secs_to_human "305"
secs_to_human "59"
secs_to_human "60"
secs_to_human "660"
secs_to_human "3000"

Output:

Time Elapsed : 5 minutes and 0 seconds.
Time Elapsed : 5 minutes and 5 seconds.
Time Elapsed : 0 minutes and 59 seconds.
Time Elapsed : 1 minutes and 0 seconds.
Time Elapsed : 11 minutes and 0 seconds.
Time Elapsed : 50 minutes and 0 seconds.

To use in a script as described in other posts (capture start point then call the function with the finish time:

start=$(date +%s)
# << performs some task here >>
secs_to_human "$(($(date +%s) - ${start}))"
1
12

Try the following code:

start=$(date +'%s') && sleep 5 && echo "It took $(($(date +'%s') - $start)) seconds"
2
  • why do people obsess with crazy one liners like this?
    – Mike Q
    Commented May 21, 2021 at 16:58
  • 1
    @MikeQ - I am glad they do! Such knowledge of available tools. We learn from these "haikus". Commented Jul 15, 2022 at 10:54
10

This is a one-liner alternative to Mike Q's function:

secs_to_human() {
    echo "$(( ${1} / 3600 ))h $(( (${1} / 60) % 60 ))m $(( ${1} % 60 ))s"
}
2
  • Nice! I usually am very verbose with my bash code, this is cool.
    – Mike Q
    Commented Feb 1, 2020 at 22:48
  • 8
    Combining this with SECONDS from Lon Kaut's answer and keeping mind that $/${} is unnecessary on arithmetic variables makes the code so short it could even be used inline: echo "$((SECONDS/3600))h $(((SECONDS/60)%60))m $((SECONDS%60))s"
    – ssc
    Commented Jun 2, 2020 at 9:34
4

This is an old post and it seems that everybody like writing too much code here. But actually, you only need 3 lines of simple bash code in order to you can show properly the elapsed time in a well-formatted way.

START_TIME=$(date +%s)

# put your code here

ELAPSED=$(($(date +%s) - START_TIME))
printf "elapsed: %s\n\n" "$(date -d@$ELAPSED -u +%H\ hours\ %M\ min\ %S\ sec)"

This produces the following result:

elapsed: 0 hours 2 min 19 sec

Test the code:

START_TIME=$(date +%s)
ELAPSED=86399
printf "elapsed: %s\n\n" "$(date -d@$ELAPSED -u +%H\ hour\ %M\ min\ %S\ sec)"

elapsed: 23 hour 59 min 59 sec

If you would like to show the days as well, then you need to do a little trick in order for the day will be displayed properly:

 ELAPSED=86399
 printf "elapsed: %s\n\n" "$(date -d@$ELAPSED -u +%d\ days\ %H\ hour\ %M\ min\ %S\ sec)"
 elapsed: 01 days 23 hour 59 min 59 sec

 ELAPSED=86400
 elapsed: 02 days 00 hour 00 min 00 sec

As you can see the day is calculated on a wrong way, so you need to do a math operation in bash this way:

printf "elapsed: %s day %s\n\n" "$(($(date -d@$ELAPSED -u +%d)-1))" "$(date -d@$ELAPSED -u +%H\ hour\ %M\ min\ %S\ sec)"
elapsed: 1 day 00 hour 00 min 00 sec

The \ character in the date-time pattern escapes the whitespace.

I hope that it helps you.

3

try using time with the elapsed seconds option:

/usr/bin/time -f%e sleep 1 under bash.

or \time -f%e sleep 1 in interactive bash.

see the time man page:

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

and

FORMATTING THE OUTPUT
...
    %      A literal '%'.
    e      Elapsed  real  (wall  clock) time used by the process, in
                 seconds.
1
  • 1
    /bin/time isn't going to work here: OP mentions a block. So we really need the keyword time here. Commented Mar 14, 2015 at 6:37
1

Combining internal variable "$SECONDS" in Lon Kaut's answer with ssc's one-liner under Internal Server Error's answer:

SECONDS=0
sleep 2s
echo "Elapsed time: $((SECONDS/3600))h $(((SECONDS/60)%60))m $((SECONDS%60))s"

($/${} is unnecessary on arithmetic variables)

-1
start=$(date +%Y%m%d%H%M%S);
for x in {1..5};
do echo $x;
sleep 1; done;
end=$(date +%Y%m%d%H%M%S);
elapsed=$(($end-$start));
ftime=$(for((i=1;i<=$((${#end}-${#elapsed}));i++));
        do echo -n "-";
        done;
        echo ${elapsed});
echo -e "Start  : ${start}\nStop   : ${end}\nElapsed: ${ftime}"

Start  : 20171108005304
Stop   : 20171108005310
Elapsed: -------------6
-4
    #!/bin/bash

    time_elapsed(){
    appstop=$1; appstart=$2

    ss_strt=${appstart:12:2} ;ss_stop=${appstop:12:2}
    mm_strt=${appstart:10:2} ;mm_stop=${appstop:10:2}
     hh_strt=${appstart:8:2} ; hh_stop=${appstop:8:2}
     dd_strt=${appstart:6:2} ; dd_stop=${appstop:6:2}
     mh_strt=${appstart:4:2} ; mh_stop=${appstop:4:2}
     yy_strt=${appstart:0:4} ; yy_stop=${appstop:0:4}

    if [ "${ss_stop}" -lt "${ss_strt}" ]; then ss_stop=$((ss_stop+60)); mm_stop=$((mm_stop-1)); fi
    if [ "${mm_stop}" -lt "0" ]; then mm_stop=$((mm_stop+60)); hh_stop=$((hh_stop-1)); fi
    if [ "${mm_stop}" -lt "${mm_strt}" ]; then mm_stop=$((mm_stop+60)); hh_stop=$((hh_stop-1)); fi
    if [ "${hh_stop}" -lt "0" ]; then hh_stop=$((hh_stop+24)); dd_stop=$((dd_stop-1)); fi
    if [ "${hh_stop}" -lt "${hh_strt}" ]; then hh_stop=$((hh_stop+24)); dd_stop=$((dd_stop-1)); fi

    if [ "${dd_stop}" -lt "0" ]; then dd_stop=$((dd_stop+$(mh_days $mh_stop $yy_stop))); mh_stop=$((mh_stop-1)); fi
    if [ "${dd_stop}" -lt "${dd_strt}" ]; then dd_stop=$((dd_stop+$(mh_days $mh_stop $yy_stop))); mh_stop=$((mh_stop-1)); fi

    if [ "${mh_stop}" -lt "0" ]; then mh_stop=$((mh_stop+12)); yy_stop=$((yy_stop-1)); fi
    if [ "${mh_stop}" -lt "${mh_strt}" ]; then mh_stop=$((mh_stop+12)); yy_stop=$((yy_stop-1)); fi

    ss_espd=$((10#${ss_stop}-10#${ss_strt})); if [ "${#ss_espd}" -le "1" ]; then ss_espd=$(for((i=1;i<=$((${#ss_stop}-${#ss_espd}));i++)); do echo -n "0"; done; echo ${ss_espd}); fi
    mm_espd=$((10#${mm_stop}-10#${mm_strt})); if [ "${#mm_espd}" -le "1" ]; then mm_espd=$(for((i=1;i<=$((${#mm_stop}-${#mm_espd}));i++)); do echo -n "0"; done; echo ${mm_espd}); fi
    hh_espd=$((10#${hh_stop}-10#${hh_strt})); if [ "${#hh_espd}" -le "1" ]; then hh_espd=$(for((i=1;i<=$((${#hh_stop}-${#hh_espd}));i++)); do echo -n "0"; done; echo ${hh_espd}); fi
    dd_espd=$((10#${dd_stop}-10#${dd_strt})); if [ "${#dd_espd}" -le "1" ]; then dd_espd=$(for((i=1;i<=$((${#dd_stop}-${#dd_espd}));i++)); do echo -n "0"; done; echo ${dd_espd}); fi
    mh_espd=$((10#${mh_stop}-10#${mh_strt})); if [ "${#mh_espd}" -le "1" ]; then mh_espd=$(for((i=1;i<=$((${#mh_stop}-${#mh_espd}));i++)); do echo -n "0"; done; echo ${mh_espd}); fi
    yy_espd=$((10#${yy_stop}-10#${yy_strt})); if [ "${#yy_espd}" -le "1" ]; then yy_espd=$(for((i=1;i<=$((${#yy_stop}-${#yy_espd}));i++)); do echo -n "0"; done; echo ${yy_espd}); fi

    echo -e "${yy_espd}-${mh_espd}-${dd_espd} ${hh_espd}:${mm_espd}:${ss_espd}"
    #return $(echo -e "${yy_espd}-${mh_espd}-${dd_espd} ${hh_espd}:${mm_espd}:${ss_espd}")
    }

    mh_days(){
    mh_stop=$1; yy_stop=$2; #also checks if it's leap year or not

    case $mh_stop in
     [1,3,5,7,8,10,12]) mh_stop=31
     ;;
     2) (( !(yy_stop % 4) && (yy_stop % 100 || !(yy_stop % 400) ) )) && mh_stop=29 || mh_stop=28
     ;;
     [4,6,9,11]) mh_stop=30
     ;;
    esac

    return ${mh_stop}
    }

    appstart=$(date +%Y%m%d%H%M%S); read -p "Wait some time, then press nay-key..." key; appstop=$(date +%Y%m%d%H%M%S); elapsed=$(time_elapsed $appstop $appstart); echo -e "Start...: ${appstart:0:4}-${appstart:4:2}-${appstart:6:2} ${appstart:8:2}:${appstart:10:2}:${appstart:12:2}\nStop....: ${appstop:0:4}-${appstop:4:2}-${appstop:6:2} ${appstop:8:2}:${appstop:10:2}:${appstop:12:2}\n$(printf '%0.1s' "="{1..30})\nElapsed.: ${elapsed}"

    exit 0


-------------------------------------------- return
Wait some time, then press nay-key...
Start...: 2017-11-09 03:22:17
Stop....: 2017-11-09 03:22:18
==============================
Elapsed.: 0000-00-00 00:00:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.