Share your knowledge at the LQ Wiki.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 04-19-2013, 04:28 PM   #1
LQ Newbie
Registered: Apr 2013
Posts: 1

Rep: Reputation: Disabled
Shell script -- Convert string to number for subtraction

Hi! I'm totally a newbie to shell scripting. I'm trying to calculate the difference between 2 numbers which grepped from a file, which gave me error because I guess they are of type string, not numeric:


start_time=$(echo ${var1#timestamp:n:}
stop_time=$(echo ${var2#timestamp:n:}

render_time=`expr $stop_time - $start_time`
which gave me error

Please help! Thanks!
Old 04-19-2013, 04:53 PM   #2
Senior Member
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 3,316

Rep: Reputation: 818Reputation: 818Reputation: 818Reputation: 818Reputation: 818Reputation: 818Reputation: 818
I believe bash only handles integers.

You might try using perl or python instead.

The conversion from string to numbers is automatic, as determined by the operator being applied to the values.
Old 04-21-2013, 02:59 PM   #3
David the H.
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1954Reputation: 1954Reputation: 1954Reputation: 1954Reputation: 1954Reputation: 1954Reputation: 1954Reputation: 1954Reputation: 1954Reputation: 1954Reputation: 1954
Please use ***[code][/code]*** tags around your code and data, to preserve the original formatting and to improve readability. Do not use quote tags, bolding, colors, "start/end" lines, or other creative techniques.

Yes, bash is integer only, as is expr, which is what you're trying to use.

(expr is completely unneeded in modern shells, btw. Everything it can do is available built in. I suggest just forgetting that it exists. )

For floating point arithmetic you need to use an external command like bc or awk.

arithmetic expressions
How can I calculate with floating point numbers instead of just integers?

You also have two completely unnecessary uses of echo (the commands for which were unclosed as well),
and $(..) is highly recommended over `..`.



render_time=$( echo "$stop_time - $start_time" | bc )

echo "$stop_time - $start_time = $render_time"

Last edited by David the H.; 04-21-2013 at 03:05 PM. Reason: corrections


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash shell script: Str(007) to int(7),increment it(8) & convert back to string(008) drwatson_droid Programming 5 12-01-2011 11:44 AM
[SOLVED] Convert string to number without 0 in the beginning nadinnne Linux - Newbie 11 06-08-2010 09:27 AM
shell script :: how to print string that appeared maximum number of times in file ?? mayankmehta83 Linux - Newbie 3 12-07-2009 08:44 AM
shell script help - subtraction?? resolute155 Programming 6 10-18-2008 07:33 AM
convert string into number, shell mystical dervish Programming 9 08-13-2007 04:32 AM

All times are GMT -5. The time now is 05:49 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration