64-bit arithmetic in scripts?

Andrew infofarmer at mail.ru
Thu Sep 30 13:08:02 PDT 2004

Dan Nelson wrote:
> In the last episode (Sep 30), Andrew said:
> > I'm counting traffic with ipfw and shell scripts. Is there a way to
> > use more than 32-bit numbers in shell arithmetic?
> POSIX only requires "signed long" support in the shell, but FreeBSD's
> expr command has a -e flag that will let it do 64-bit math:
> $ echo $(( 65536*65536 ))
> 0
> $ echo $(expr 65536 "*" 65536)
> 0
> $ echo $(expr -e 65536 "*" 65536)
> 4294967296
> bash, ksh93 (but not pdksh), and zsh's shell arithmetic are all
> also.

Thanks! I haven't thought about using expr.

How come that my expr(1) manpage has nothing to say about -e option? In
fact my expr(1) does not accept it. I have FreeBSD 4.10. I've just
looked into a current manpage from www.freebsd.org, and it says
something about 4.x compatibility.

What is the best way to go if I need to write scripts now, but I'm
planning to switch to 5.x later? Can I upgrade expr(1) now? If not, what
should I do?

Thanks again and regards,
Andrew P.

More information about the freebsd-questions mailing list