Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I wrote a shell script which calculates the execution time of itself but the problem I'm facing is its not able to send the time difference via email. Please Help.

Here is the Script


start=$(date +%s.%N)

ls /etc > /tmp/x

rm /tmp/x

end=$(date +%s.%N)
diff=$(echo "$end-$start" | bc)
echo "Inside function $diff"

#Email Function
Message="sendmail function $diff"

#Sendmail Function
sendmailfun() {
sendmail -F $Sender -it <<END_MESSAGE
To: $Recpt
Subject: $Sub


echo "outside function $diff"

When I run this script the output on the cmd line is like follows

root@itpl-lptp-008:/home/incaendo/Downloads# sh 
Inside function .008025182
outside function .008025182

But the email I get after executing the above script just have the line as follows

sendmail function i.e. without the $diff value. why so? Is there anyway I can send the time difference via email.

share|improve this question
up vote 2 down vote accepted

The statement

Message="sendmail function $diff"

evaluates once when the script is executed. Then diff is "", so Message is sendmail function. The statement is not reexecuted after you set diff to a new value, so sendmail function is what gets emailed.

Wouldn't it be simpler to just run

time | mail -s "Test"
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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