LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
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


Reply
  Search this Thread
Old 07-12-2009, 01:36 AM   #1
ahmedb72
Member
 
Registered: Jan 2006
Location: Sydney
Distribution: RHEL
Posts: 72

Rep: Reputation: 15
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.
 
Old 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: Reputation: 60
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.
 
Old 07-12-2009, 02:49 AM   #3
ahmedb72
Member
 
Registered: Jan 2006
Location: Sydney
Distribution: RHEL
Posts: 72

Original Poster
Rep: Reputation: 15
What if I have a decimal number starting with 0, how can I let the shell deals with it as a decimal, not Octal?
 
Old 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: Reputation: 282Reputation: 282Reputation: 282
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:~$
 
Old 07-12-2009, 06:13 AM   #5
ahmedb72
Member
 
Registered: Jan 2006
Location: Sydney
Distribution: RHEL
Posts: 72

Original Poster
Rep: Reputation: 15
Thanks Wim for the nice link.

Every time I post a question here, I learn something new.
 
Old 07-12-2009, 06:37 AM   #6
tredegar
LQ 5k Club
 
Registered: May 2003
Location: London, UK
Distribution: Debian "Jessie"
Posts: 6,085

Rep: Reputation: 398Reputation: 398Reputation: 398Reputation: 398
Quote:
Code:
#var=13
#var2=$[$var1+1]
#echo $var2
12			<-- increment fails
And in this case, "increment fails" because var1 != var
 
  


Reply


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
how to auto increment 13 digit hexadecimal using script hchoonbeng Linux - Newbie 1 09-25-2008 12:03 AM
Increment backup using Dump blckspder Linux - Server 1 09-20-2008 11:49 AM
create and then Increment file by 1 nepcw Linux - Software 2 08-25-2008 04:44 PM
How do i increment the output of sed nerdtv Programming 2 12-09-2007 05:05 PM
increment in hex snutz411 Linux - Newbie 2 11-01-2005 06:54 PM


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

Main Menu
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.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration