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.
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:
and
You are trying to execute the number in the
ENDTIME
as a command. You should also see an error like1370306857: command not found
. Instead use the arithmetic expansion:You could also save the commands in a separate script,
commands.sh
, and use time command: