help-bash
[Top][All Lists]
Advanced

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

Re: [Help-bash] set -u in an arithmetic context


From: Bill Gradwohl
Subject: Re: [Help-bash] set -u in an arithmetic context
Date: Thu, 26 Apr 2012 10:14:00 -0600

On Wed, Apr 25, 2012 at 7:45 PM, DJ Mills <address@hidden> wrote:

First of all, the best way to deal with this is to stop using set -u
and handle stuff yourself.

How? I know there's antagonism towards -u but I don't understand why. I looked on Greg's FAQ site but didn't see anything of note.

The set -u feature is one of the few ways bash helps locate potentially bad code. Especially when working with arrays, where one variable name may represent numerous elements, discovering that a particular element is unset is good information. How would you find it without -u?

Without -u, when doing subscript arithmetic, and doing it wrong to point at a bogus element via a logic error causes grief down the line when -u helps locate the error quickly.

After development is over, removing -u is reasonable, but as it shouldn't hurt a script that's declared to be production ready, I see no harm in leaving -u turned on.

Finding out that -u doesn't work unless the variable is referenced with $ or ${} is a disappointment.
 
unset i
((! i)) && echo "yes"
yes

((! $i)) && echo "yes"
bash: ((: ! : syntax error: operand expected (error token is "! ")


I would prefer a failure in both cases.



--
Bill Gradwohl


reply via email to

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