1 |
Alexandre Rostovtsev posted on Sat, 25 Aug 2012 17:04:36 -0400 as |
2 |
excerpted: |
3 |
|
4 |
> [[ -n "${VALA_API_VERSION}" ]] || die "VALA_API_VERSION not set" |
5 |
|
6 |
It's just style, but... |
7 |
|
8 |
1) [[ ]] manages non-word characters (whitespace, etc) hidden behind |
9 |
variables, so quotes are only necessary if they appear in string- |
10 |
literals, not the case here. I know eliminating the quotes is gentoo |
11 |
policy as I've seen it suggested many times before. |
12 |
|
13 |
2) Gentoo policy regarding implied -n? I know gentoo policy prefers {} |
14 |
cuddled varnames to make the varname explicit, but does it prefer |
15 |
explicit -n as well, since that's implicit test behavior and thus does |
16 |
not need to be explicitly specified? |
17 |
|
18 |
To my way of thinking [[ ${var} ]] is even clearer than [[ -n ${var} ]] |
19 |
since there's only one way to interpret the former and I have to |
20 |
correctly parse the -n (as opposed to -any-other-letter) given the |
21 |
latter. They're even listed together in bash's "help test" output, so |
22 |
are considered to have identical behavior to the point of being listed |
23 |
together by bash itself. |
24 |
|
25 |
Incorporating both suggestions: |
26 |
|
27 |
[[ ${VALA_API_VERSION} ]] || die "VALA_API_VERSION not set" |
28 |
|
29 |
But regardless of #2, definitely eliminate the quotes inside the [[ ]], |
30 |
as I've seen that suggested numerous times in other cases. The better |
31 |
quote handling regarding variables is in fact one of the reasons the |
32 |
[[ ]] form is preferred to the POSIX compliant [ ] form. |
33 |
|
34 |
-- |
35 |
Duncan - List replies preferred. No HTML msgs. |
36 |
"Every nonfree program has a lord, a master -- |
37 |
and if you use the program, he is your master." Richard Stallman |