Visit Jeremy's Blog.
 Home Forums HCL Reviews Tutorials Articles Register Search Today's Posts Mark Forums Read
 LinuxQuestions.org Two-digit integer value doesn't increment
 User Name Remember Me? Password
 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!

Notices

 07-12-2009, 12:36 AM #1 ahmedb72 Member   Registered: Jan 2006 Location: Sydney Distribution: RHEL Posts: 72 Rep: Two-digit integer value doesn't increment Hi, When I assigned "13" or "013" to a variable, incrementing it didn't work. Below is what I got: Code: ```#var1=5 #var2=\$[\$var1+1] #echo \$var2 6 <-- increment succeeds #var1=05 #var2=\$[\$var1+1] #echo \$var2 6 <-- increment succeeds #var1=005 #var2=\$[\$var1+1] #echo \$var2 6 <-- increment succeeds #var2=\$[13+1] #echo \$var2 14 <-- increment succeeds #var=13 #var2=\$[\$var1+1] #echo \$var2 12 <-- increment fails #var1=013 #var2=\$[\$var1+1] #echo \$var2 12 <-- increment fails``` Thanks in advance.
 07-12-2009, 01:07 AM #2 Uncle_Theodore Member   Registered: Dec 2007 Location: Charleston WV, USA Distribution: Slackware 12.2, Arch Linux Amd64 Posts: 896 Rep: Cute! 013 starts with a 0, so is an octal, its decimal value is 1*8 + 3 = 11 And of course, 11 + 1 is 12, since the value of var2 is printed in decimals, not in octals. When a number starts with a zero, it's assumed to be written in base 8 (octals). When it starts with 0x, it's assumed to be written in hexidecimals (base 16). Try these echo \$((0x11+1)) echo \$((0xaa+1)) Last edited by Uncle_Theodore; 07-12-2009 at 01:16 AM.
 07-12-2009, 01:49 AM #3 ahmedb72 Member   Registered: Jan 2006 Location: Sydney Distribution: RHEL Posts: 72 Original Poster Rep: What if I have a decimal number starting with 0, how can I let the shell deals with it as a decimal, not Octal?
 07-12-2009, 03:54 AM #4 Wim Sturkenboom Senior Member   Registered: Jan 2005 Location: Roodepoort, South Africa Distribution: Slackware 10.1/10.2/12, Ubuntu 12.04, Crunchbang Statler Posts: 3,786 Rep: http://linuxgazette.net/issue18/bash.html Search for trim Code: ```wim@desktop1:~\$ var1=013 wim@desktop1:~\$ echo \$var1 013 wim@desktop1:~\$ echo \${var1#0} 13 wim@desktop1:~\$```
 07-12-2009, 05:13 AM #5 ahmedb72 Member   Registered: Jan 2006 Location: Sydney Distribution: RHEL Posts: 72 Original Poster Rep: Thanks Wim for the nice link. Every time I post a question here, I learn something new.
 07-12-2009, 05:37 AM #6 tredegar LQ 5k Club   Registered: May 2003 Location: London, UK Distribution: Debian "Jessie" Posts: 6,087 Rep: Quote: Code: ```#var=13 #var2=\$[\$var1+1] #echo \$var2 12 <-- increment fails``` And in this case, "increment fails" because var1 != var

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

 Similar Threads Thread Thread Starter Forum Replies Last Post hchoonbeng Linux - Newbie 1 09-24-2008 11:03 PM blckspder Linux - Server 1 09-20-2008 10:49 AM nepcw Linux - Software 2 08-25-2008 03:44 PM nerdtv Programming 2 12-09-2007 04:05 PM snutz411 Linux - Newbie 2 11-01-2005 05:54 PM

LinuxQuestions.org

All times are GMT -5. The time now is 09:47 AM.

 Contact Us - Advertising Info - Rules - LQ Merchandise - Donations - Contributing Member - LQ Sitemap -
 Advertisement
 My LQ
 Write for LQ LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
 Syndicate Latest Threads   LQ News Twitter: @linuxquestions Facebook: linuxquestions Google+: linuxquestions