Gentoo Archives: gentoo-dev

From: Roy Marples <uberlord@g.o>
To: gentoo-dev@l.g.o
Subject: Re: [gentoo-dev] Re: Re: RFC: sh versionator.eclass
Date: Tue, 02 Oct 2007 19:56:20
Message-Id: 1191354262.22700.6.camel@uberpc.marples.name
In Reply to: [gentoo-dev] Re: Re: RFC: sh versionator.eclass by Steve Long
1 On Tue, 2007-10-02 at 19:37 +0100, Steve Long wrote:
2 > Roy Marples wrote:
3 > > With [[ ]] you don't need to quote, which encourages people not to
4 > > bother learning when and when not to quote.
5 > >
6 > Ugh, that is so untrue imo. In #bash most people don't know [[ and use [
7 > without quoting, as well as leaving out quotes elsewhere. Which is why we
8 > beat them over the head about quoting _all_ the time (!quotes in #bash [1])
9
10 You should always quote when using [ - the only exception you can ever
11 make is when using something like $? or $# as that is always a number.
12
13 >
14 > > Regular readers of -dev will also notice a large amount of people not
15 > > quoting correctly, my view is that bash encourages this through the use
16 > > of [[ ]].
17 > >
18 > A lot of those have been stuff like cd $S (when S could have spaces
19 > according to where the tmp dir is located) and the like, ie stuff outside
20 > [[. Honestly, [[ has got little to do with learning about quoting, since
21 > the typical case where quoting is missing is when a path is passed to a
22 > command. The thing to learn is that the shell expands everything before
23 > passing it to a command, and that spaces will lead to two parameters where
24 > you thought you had one, unless you use quotes. (ie cd "$S" )
25
26 Everything in shell IS a command with the exception of variable
27 assignment. Why do you think [ exist in /usr/bin?
28
29 Once you think "everything is a command" then quoting becomes a lot
30 easier. [[ ]] removes this concept.
31
32 > Inside [[ is similar to assignment, in that it's down to the lex/parse (eg
33 > a=' omg spaces!'; b='oh Noes! '$a; echo "$b" # works fine -- but note
34 > what happens when you echo $b with no quotes.) kojiro knows more about the
35 > corner cases where quoting is needed in [[, but it _never_ shields you from
36 > quoting issues outside tests, and you *will* be told about it if you paste
37 > an unsafe loc in #bash. (We don't care if that's not the current problem as
38 > you see it: if you're not quoting right, your scripts are worse than
39 > useless, since they'll break when your users least expect it, for no
40 > apparent reason, and you're nowhere to be found. People soon learn not to
41 > paste unsafe expansions ;p and they get into the habit of always quoting.)
42 >
43 > [1] USE MORE QUOTES! Read
44 > http://bash-hackers.org/wiki/doku.php?id=syntax:words to understand why.
45 > http://www.grymoire.com/Unix/Quote.html will tell you how. -- Optimally,
46 > you should quote every parameter expansion ($foo).
47
48 So in other words, [ ] always needs quotes whereas [[ ]] normally
49 doesn't but could need quotes in corner cases.
50
51 I know which I'd prefer - certainty over uncertainty.
52
53 Thanks
54
55 Roy
56
57 --
58 gentoo-dev@g.o mailing list

Replies

Subject Author
[gentoo-dev] Re: Re: Re: RFC: sh versionator.eclass Steve Long <slong@××××××××××××××××××.uk>