Subject: Re: Arithmetic, octal and leading zeroes in sh
To: matthew sporleder <msporleder@gmail.com>
From: David Laight <david@l8s.co.uk>
List: netbsd-users
Date: 10/14/2005 20:57:18
On Thu, Oct 13, 2005 at 12:52:14PM -0400, matthew sporleder wrote:
> You should use expr in sh scripts to do arithmatic:
> $>echo `expr 08 + 1`
> 9
> $>echo `expr 09 + 1`
> 10
> $>echo `expr 010 + 1`
> 11
Not it you want scripts to run quickly.....
Some alternatives:
$ x=08; echo ${x#0} # will strip a single leading zero.
$ x=08; echo $((${x#0}+2)) # outputs 10
$ x=0019; echo $((1$x-10000)) # outputs 19
David
--
David Laight: david@l8s.co.uk