Gentoo Archives: gentoo-dev

From: Steven J Long <slong@××××××××××××××××××.uk>
To: gentoo-dev@l.g.o
Subject: [gentoo-dev] Re: [RFC] obs eclasses
Date: Tue, 20 Sep 2011 05:08:04
Message-Id: j596vc$7qe$
In Reply to: Re: [gentoo-dev] [RFC] obs eclasses by Joshua Kinard
Joshua Kinard wrote:

> On 09/13/2011 07:24, Amadeusz Żołnowski wrote: > >> You don't need -n/-z with [[. >> >> [[ $var ]] == [[ -n $var ]] >> [[ ! $var ]] == [[ -z $var ]] >>
Also, you can usually be more succinct with [[ $var ]] || { some code; } for the empty case (as opposed to [[ $var ]] && { something else; } for code run when var is non-empty.)
> What about other comparisons, like -f, -e, or -d? Bash's manpage only > says > [[ expression ]] -- it doesn't seem to go into the level of detail (at > least not in the section that I quickly perused) about what flag > operators are necessary or not. >
As Amadeusz said, you can't omit the other ones.
> Also, is this a bash4-only thing, or bash3 and/or bash2 as well? >
It was definitely around in all bash-3 versions, from experience, and it's also a defined behaviour for POSIX sh test or [, tho only guaranteed for XSI systems[1] so I'd be surprised if it weren't in bash-2.
> If yes to above, we should get this edited and fixed up, then, because it > uses -z/-n inside [[ ]]: > >
As Michal said, it doesn't do any harm. imo it'd be better just to add that [[ $var ]] is the same as [[ -n $var ]]. [[ ! $var ]] doesn't seem better than [[ -z $var ]] to my eyes; the latter is clearer imo. (Decrufting ${var} to $var would be more of a help for learners imo; you only need to wrap a simple variable expansion in {} when it's immediately followed by an alphanumeric or underscore character, which would get interpreted as part of its name, which any syntax highlighting editor will show you. In several years of BASH scripting I've only once had an issue with that, in some complex text output.)
> Oh, forgot, it won't break initscripts, will it? >
Well you wouldn't use [[ in a sh-compatible initscript in any case. There it'd be safest to stick to [ -n "$var" ] (or -z ofc.) [1] -- #friendly-coders -- We're friendly, but we're not /that/ friendly ;-)