bug-coreutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: expr integer format always decimal?


From: Bob Proulx
Subject: Re: expr integer format always decimal?
Date: Sat, 11 Sep 2004 21:20:32 -0600
User-agent: Mutt/1.3.28i

Andreas Schwab wrote:
> Bob Proulx writes:
> > Paul Eggert wrote:
> >> So, under your interpretation, "expr 010" would print 8, but "expr 010
> >> + 0" would print 10?  That doesn't sound right to me.
> >
> > My question was why couldn't a conforming implementation interpret 010
> > as octal and therefore print a decimal 8?
> 
> 010 is surely a valid decimal number.

Ha!  (read with a smile)  'printf' does not think so.  Try these:

  printf "%d\n" 010
  8

  printf "%d\n" 08  
  bash: printf: 08: invalid number
  0

  printf "%d\n" 09
  bash: printf: 09: invalid number
  0

That is what started me down this discussion.  Surely what is good for
the goose is good for the gander.  But as you can see it is not.

You can use expr to clean up the leading zero as in the following:

  printf "%d\n" $(expr 0 + 08)
  8

But then the question became is expr always guaranteed by the standard
to read arguments as decimal integer constants.  Especially because
printf is defined to read them as octal and hex depending upon their
prefix.

I know awk is defined to interpret values as decimals.  The standard
says so explicitly.  So I know I can use awk this way, even if it is a
a little more involved.

  awk 'BEGIN{printf "%d\n", ARGV[1];}' 08
  8

Bob




reply via email to

[Prev in Thread] Current Thread [Next in Thread]