Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 13 Oct 2014 14:13:09 +0100
From:      RW <rwmaillists@googlemail.com>
To:        freebsd-questions@freebsd.org
Subject:   Re: sh man page ....
Message-ID:  <20141013141309.16b0edb8@gumby.homeunix.com>
In-Reply-To: <543BC227.50004@hiwaay.net>
References:  <5437FB8B.9080008@hiwaay.net> <20141010183814.3ae32a05@gumby.homeunix.com> <5438755B.2000108@hiwaay.net> <20141013124649.4082d94f@gumby.homeunix.com> <543BC227.50004@hiwaay.net>

next in thread | previous in thread | raw e-mail | index | archive | help
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@freebsd.org mailing list
> > http://lists.freebsd.org/mailman/listinfo/freebsd-questions
> > To unsubscribe, send any mail to
> > "freebsd-questions-unsubscribe@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.




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20141013141309.16b0edb8>