sh man page ....

RW rwmaillists at googlemail.com
Mon Oct 13 13:13:18 UTC 2014


On Mon, 13 Oct 2014 07:14:31 -0500
William A. Mahaffey III wrote:

> On 10/13/14 06:46, RW wrote:
> > On Fri, 10 Oct 2014 19:10:03 -0500

> > The problem here is that you have:
> >
> >    [ 0 -lt $(($slept)) ]
> >
> > If you change it to the normal usage
> >
> >    [ 0 -lt $((slept)) ]
> >
> > it works as expected.
> >
> > Is there any particular reason for the extra "$"?
> >
> > I guess the difference is not in the handling of uninitialised
> > variables, but specifically in the handling of $(()) which is an
> > error in sh, but not is bash.
> > _______________________________________________
> > freebsd-questions at freebsd.org mailing list
> > http://lists.freebsd.org/mailman/listinfo/freebsd-questions
> > To unsubscribe, send any mail to
> > "freebsd-questions-unsubscribe at freebsd.org"
> >
> 
> Good question. I am *not* a bash/shell wiz, but I think the extra "$" 
> was needed to get bash to behave (Linux, FC14 64-bit, i.e. a bit
> dated). Not 100% on that, but pretty sure ....

I suspect you are mixing-up the inner and outer "$". $((slept)) will
evaluate to a number whether or not slept is defined, which is what is
needed. I can't see that the inner "$" in  $(($slept) does anything
useful in bash.

Just to be clear what I think is happening is that $(($slept) is
equivalent to $((slept)) when slept is defined and $(()) when it isn't. 

And in bash

   $ echo $(( x  ))
   0
   $ echo $((   ))
   0

and sh
   $ echo $(( x  ))
   0
   $ echo $((    ))
   arithmetic expression: expecting primary: "    "


This is a much less significant difference than the handling of
uninitialized variables would be.

$x in POSIX arithmetic is almost always going to be a mistake and one
that may produce hard to spot bugs, so I think treating $((    )) as
an error is very sensible.



More information about the freebsd-questions mailing list