On Tuesday, January 15, 2013 03:20:20 AM Duncan wrote:
> Mike Frysinger posted on Mon, 14 Jan 2013 17:09:51 -0500 as excerpted:
> 
> >>> + [[ ${BUILD_DIR} ]] || die "${FUNCNAME}: BUILD_DIR not set."
> >> 
> >> really should use -n there
> > 
> >> Doesn't matter.
> > 
> > the point wasn't "will it work".  it's more "how easy is it to glance at
> > code and know what it is doing".
> 
> Indeed.  But arguably standalone [[ ${var} ]] tests ARE easier to "know 
> what it doing."  

While I agree, I wouldn't lose sleep over it. Both are perfectly acceptable 
and nearly equal in clarity. [[ $x ]] is probably most common.

> Consider:
> ...
> 4) You are arguing the "at a glance" position, but didn't even MENTION 
> the needlessly negated logic of [[ -n $string ]] || ... , which could be 
> rewritten to avoid the || negation as [[ -z $string ]] && ...

-z is the negation. -n is the default. [[ $x ]] is rewritten internally as [[ 
-n $x ]]. [[ ! $x ]] == [[ ! -n $x ]] == [[ -z $x ]]. It's perfectly logical 
that this expression evaluates true when given a non-empty value. As ''expr || 
die'' is the most expected form for error-checking, negating this with ''-z'' 
or otherwise makes little sense.

"Empty == False" is not an uncommon concept. e.g. python -c 'print(bool(""))'

> 5) [[ ]] is already a bashism while the standalone string test is common 
> shell.  Surely you're not arguing that people familiar enough with the 
> [[ ]] || construct to parse it at a glance can't equally capably parse 
> the a standalone string test, given its use in non-bash shell context as 
> well.

I wish people would stop referring to features that predate both Bash and 
POSIX as "Bashisms". ''[['' is superior to and easier to understand than 
''[''. Everyone should be familiar with both. As only Bash is relevant here, 
''test'' and ''['' are mostly irrelevant. With few exceptions, ''[['' should 
be preferred when available (and in this case, guaranteed available).

> Obviously the example in #6 is taking it a bit far, but my point is, an 
> explicit (if one-of-many) -z/-n test vs. the standalone (single-case) 
> $string test really is personal preference.

The performance difference is considerable.

-- 
Dan Douglas

Attachment: signature.asc
Description: This is a digitally signed message part.

Reply via email to