1 |
Joshua Kinard wrote: |
2 |
|
3 |
> On 09/13/2011 07:24, Amadeusz Żołnowski wrote: |
4 |
> |
5 |
>> You don't need -n/-z with [[. |
6 |
>> |
7 |
>> [[ $var ]] == [[ -n $var ]] |
8 |
>> [[ ! $var ]] == [[ -z $var ]] |
9 |
>> |
10 |
Also, you can usually be more succinct with [[ $var ]] || { some code; } for |
11 |
the empty case (as opposed to [[ $var ]] && { something else; } for code run |
12 |
when var is non-empty.) |
13 |
|
14 |
> What about other comparisons, like -f, -e, or -d? Bash's manpage only |
15 |
> says |
16 |
> [[ expression ]] -- it doesn't seem to go into the level of detail (at |
17 |
> least not in the section that I quickly perused) about what flag |
18 |
> operators are necessary or not. |
19 |
> |
20 |
As Amadeusz said, you can't omit the other ones. |
21 |
|
22 |
> Also, is this a bash4-only thing, or bash3 and/or bash2 as well? |
23 |
> |
24 |
It was definitely around in all bash-3 versions, from experience, and it's |
25 |
also a defined behaviour for POSIX sh test or [, tho only guaranteed for XSI |
26 |
systems[1] so I'd be surprised if it weren't in bash-2. |
27 |
|
28 |
> If yes to above, we should get this edited and fixed up, then, because it |
29 |
> uses -z/-n inside [[ ]]: |
30 |
> http://devmanual.gentoo.org/tools-reference/bash/index.html |
31 |
> |
32 |
As Michal said, it doesn't do any harm. imo it'd be better just to add that |
33 |
[[ $var ]] is the same as [[ -n $var ]]. [[ ! $var ]] doesn't seem better |
34 |
than [[ -z $var ]] to my eyes; the latter is clearer imo. |
35 |
|
36 |
(Decrufting ${var} to $var would be more of a help for learners imo; you |
37 |
only need to wrap a simple variable expansion in {} when it's immediately |
38 |
followed by an alphanumeric or underscore character, which would get |
39 |
interpreted as part of its name, which any syntax highlighting editor will |
40 |
show you. In several years of BASH scripting I've only once had an issue |
41 |
with that, in some complex text output.) |
42 |
> Oh, forgot, it won't break initscripts, will it? |
43 |
> |
44 |
Well you wouldn't use [[ in a sh-compatible initscript in any case. There |
45 |
it'd be safest to stick to [ -n "$var" ] (or -z ofc.) |
46 |
|
47 |
[1] http://pubs.opengroup.org/onlinepubs/009695399/utilities/test.html |
48 |
-- |
49 |
#friendly-coders -- We're friendly, but we're not /that/ friendly ;-) |