From: janis_papanagnou+ng@hotmail.com   
      
   On 11.10.2025 02:02, Kaz Kylheku wrote:   
   > On 2025-10-10, BGB wrote:   
   >> Yeah, '0' by itself indicating octal is weird, so I might agree here.   
   >> 123 //decimal   
   >> 0123 //maybe reinterpret as decimal?   
   >> 0o123 //octal   
   >> 0x123 //hexadecimal   
   >> 0b101 //binary   
   >   
   > Lisp people worked this out before the end of the 80s:   
   >   
   > [1]> 777   
   > 777   
   > [2]> 00777   
   > 777   
   > [3]> #o777   
   > 511   
   > [4]> #x777   
   > 1911   
   > [5]> #b1001   
   > 9   
   >   
   > Leading zeros changing base is really a sneaky stupidity, and causes   
   > problems in shell scripts also, from time to time.   
   >   
   > $ printf "%d\n" 0777   
   > 511   
   > $   
   > $ echo $(( 0777 + 0 ))   
   > 511   
   >   
      
   Yes, indeed. And behavior between shells and versions differs as well.   
      
   $ dash -c 'printf "%d\n" 077'   
   63   
   $ ksh93u -c 'printf "%d\n" 077'   
   63   
   $ ksh93u+ -c 'printf "%d\n" 077'   
   77   
      
   Now, is that good that ksh has fixed that? (I have my doubts.)   
      
   Also if you get actual values from variable expansion (as opposed to   
   constant literals) you may get surprises.   
      
   At some point I used [in Kornshell] often explicit "base specifiers"   
   (which is not generally available in shells), base#number   
      
   $ ksh93u -c 'printf "%d\n" 10#077'   
   77   
      
   especially sensible if used with variables containing arbitrary number   
   formats where the leading zero is hidden in 'var'.   
      
   $ ksh93u -c 'var=077; printf "%d\n" 10#$var $var'   
   77   
   63   
      
   Leading zero octals is yet another badly designed language feature,   
   and not only in the shell language - but that ship has sailed...   
      
   Janis   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   
|