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, 03: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, 03:53 PM   #2
Senior Member
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,715

Rep: Reputation: 1281Reputation: 1281Reputation: 1281Reputation: 1281Reputation: 1281Reputation: 1281Reputation: 1281Reputation: 1281Reputation: 1281
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, 01:59 PM   #3
David the H.
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian + kde 4 / 5
Posts: 6,837

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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 02: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 10:44 AM
[SOLVED] Convert string to number without 0 in the beginning nadinnne Linux - Newbie 11 06-08-2010 08:27 AM
shell script :: how to print string that appeared maximum number of times in file ?? mayankmehta83 Linux - Newbie 3 12-07-2009 07:44 AM
shell script help - subtraction?? resolute155 Programming 6 10-18-2008 06:33 AM
convert string into number, shell mystical dervish Programming 9 08-13-2007 03:32 AM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:18 PM.

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