Share your knowledge at the LQ Wiki.
 Home Forums HCL Reviews Tutorials Articles Register Search Today's Posts Mark Forums Read
 LinuxQuestions.org Converting string to integer in BASH
 Programming This forum is for all programming questions. The question does not have to be directly related to Linux and any language is fair game.

Notices

 04-24-2010, 11:59 AM #16 gnashley Amigo developer   Registered: Dec 2003 Location: Germany Distribution: Slackware Posts: 4,855 Rep: The portable way to do the math is like this: var=\$((\$var-1)) These are all less portable: var=((var-1)) var=\$((var-1)) ((var--)) let...
 08-31-2010, 07:23 PM #17 EricAiken LQ Newbie   Registered: Oct 2003 Posts: 1 Rep: Ok, can someone explain this one? \$ a="06" \$ b="1" \$ c=\$((a+b)) \$ echo \$c 7 \$ a="07" \$ b="1" \$ c=\$((a+b)) \$ echo \$c 8 \$ a="08" \$ b="1" \$ c=\$((a+b)) -sh: 08: value too great for base (error token is "08") \$ a="09" \$ b="1" \$ c=\$((a+b)) -sh: 09: value too great for base (error token is "09") \$ a="10" \$ b="1" \$ c=\$((a+b)) \$ echo \$c 11
08-31-2010, 08:38 PM   #18
jay73
LQ Guru

Registered: Nov 2006
Location: Belgium
Distribution: Ubuntu 11.04, Debian testing
Posts: 5,019

Rep:
Quote:
 \$ a="08" \$ b="1" \$ c=\$((a+b)) -sh: 08: value too great for base (error token is "08") \$ a="09" \$ b="1" \$ c=\$((a+b)) -sh: 09: value too great for base (error token is "09")
Prefacing a number with 0 makes it octal. Your problem is that octal uses units of 8, expressed as the range 0-7, so 08 is plain nonsense. Decimal 8 would be octal (0)10 and decimal 9 would be octal (0)11.

echo \$((8#10)) <--- base 8 (octal) value 10 is decimal 8; this will print 8
echo \$((2#10)) <--- base 2 (binary) 10 is decimal 2; it will print 2
echo \$((16#10)) <--- base 16 (hexadecimal) 10 is decimal 16; it will print 16

Last edited by jay73; 08-31-2010 at 08:46 PM.

2 members found this post helpful.

 Tags bash, conversion, convert, integer, string, type

 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 Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post dziadgba Linux - Newbie 5 08-31-2009 05:59 PM MicahCarrick Programming 4 12-19-2005 02:16 PM Bean101 Programming 2 05-27-2004 04:46 AM woodywellhung Programming 3 04-24-2004 05:27 PM JStew Programming 3 12-03-2002 08:53 AM

LinuxQuestions.org

All times are GMT -5. The time now is 07:36 PM.

 Contact Us - Advertising Info - Rules - LQ Merchandise - Donations - Contributing Member - LQ Sitemap -