Share your knowledge at the LQ Wiki.
 Home Forums HCL Reviews Tutorials Articles Register Search Today's Posts Mark Forums Read
 LinuxQuestions.org Two-digit integer value doesn't increment
 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, 01: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, 02: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 02:16 AM.
 07-12-2009, 02: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, 04: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, 06: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, 06:37 AM #6 tredegar LQ 5k Club   Registered: May 2003 Location: London, UK Distribution: Debian "Jessie" Posts: 6,085 Rep: Quote: Code: ```#var=13 #var2=\$[\$var1+1] #echo \$var2 12 <-- increment fails``` And in this case, "increment fails" because var1 != var

 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-25-2008 12:03 AM blckspder Linux - Server 1 09-20-2008 11:49 AM nepcw Linux - Software 2 08-25-2008 04:44 PM nerdtv Programming 2 12-09-2007 05:05 PM snutz411 Linux - Newbie 2 11-01-2005 06:54 PM

All times are GMT -5. The time now is 06:35 AM.

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