Why 08, 09 cannot be used as 8 ,9?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • arthurzzk
    New Member
    • Feb 2007
    • 8

    Why 08, 09 cannot be used as 8 ,9?

    If you do $((08+1)) or $((09+1)) in terminal, linux shows an error.
    However, you can use 07,06,05,04,03, 02,01,00, and Linux treat them as 7,6,5,4,3,2,1,0 .

    Can someone tell me how to make 08 and 09 work in this way?
  • Motoma
    Recognized Expert Specialist
    • Jan 2007
    • 3236

    #2
    Originally posted by arthurzzk
    If you do $((08+1)) or $((09+1)) in terminal, linux shows an error.
    However, you can use 07,06,05,04,03, 02,01,00, and Linux treat them as 7,6,5,4,3,2,1,0 .

    Can someone tell me how to make 08 and 09 work in this way?
    That's easy!
    Prefacing a number with a 0 when it isn't the only digit before a decimal (.) will cause most programming languages to treat is as Octal (base 8). The highest numeric digit in octal is 7, thus 8 and 9 will not show up.

    If you don't believe me, try 077 and see what you get.

    Comment

    • arthurzzk
      New Member
      • Feb 2007
      • 8

      #3
      can someone tell me how to make 08 and 09 work as 8, 9?

      Comment

      • Motoma
        Recognized Expert Specialist
        • Jan 2007
        • 3236

        #4
        Originally posted by arthurzzk
        can someone tell me how to make 08 and 09 work as 8, 9?
        As far as I know, this cannot be done in the way you've posed the question. This is similar to asking for 0x11 be represented as 11 (which is certainly not the case).

        Comment

        • ValHolla
          New Member
          • Sep 2006
          • 8

          #5
          if you need them to be viewed as 08 and 09 you can do your calculations with the decimal equivalents 0 - 9 and use printf to display them with the leading 0 (zero)

          Comment

          • michaelb
            Recognized Expert Contributor
            • Nov 2006
            • 534

            #6
            This behavior seems to vary depending on the shell:
            Code:
            ~> echo $SHELL
            /usr/local/bin/bash
            ~> echo $((09+7))
            -bash: 09: value too great for base (error token is "09")
            ~> ksh
            $  echo $((09+7))
            16
            $
            There's also the typeset built-in which can be used for formatting:
            Code:
            $ 
            $ typeset -Z2 num1=9  
            $ typeset -Z3 num2   
            $ num2=8
            $ echo $num1
            09
            $ echo $num2
            008
            $ echo $((num1 * num2))
            72
            $

            Comment

            • shamrockjade
              New Member
              • Dec 2009
              • 1

              #7
              This worked for me in the bash shell. If I tried to default the dirNUM to 3 digits first then I would error out with the 08 errors.
              Code:
                    dirNUM=1
                    until [[ $dirNUM == 367 ]]
                    do
                       # Change directory number to 3 digits
                       if [[ ! -d ${root_dir}/$(printf "%.3d" $dirNUM) ]]
                       then
                          mkdir ${root_dir}/$(printf "%.3d" $dirNUM)
                       fi
                       ((dirNUM+=1))
                    done
              Last edited by Frinavale; Jan 6 '10, 08:32 PM. Reason: Please post code in [code] ... [/code] tags. Added code tags.

              Comment

              Working...