On Tue, 2007-10-02 at 19:37 +0100, Steve Long wrote: > Roy Marples wrote: > > With [[ ]] you don't need to quote, which encourages people not to > > bother learning when and when not to quote. > > > Ugh, that is so untrue imo. In #bash most people don't know [[ and use [ > without quoting, as well as leaving out quotes elsewhere. Which is why we > beat them over the head about quoting _all_ the time (!quotes in #bash [1])
You should always quote when using [ - the only exception you can ever make is when using something like $? or $# as that is always a number. > > > Regular readers of -dev will also notice a large amount of people not > > quoting correctly, my view is that bash encourages this through the use > > of [[ ]]. > > > A lot of those have been stuff like cd $S (when S could have spaces > according to where the tmp dir is located) and the like, ie stuff outside > [[. Honestly, [[ has got little to do with learning about quoting, since > the typical case where quoting is missing is when a path is passed to a > command. The thing to learn is that the shell expands everything before > passing it to a command, and that spaces will lead to two parameters where > you thought you had one, unless you use quotes. (ie cd "$S" ) Everything in shell IS a command with the exception of variable assignment. Why do you think [ exist in /usr/bin? Once you think "everything is a command" then quoting becomes a lot easier. [[ ]] removes this concept. > Inside [[ is similar to assignment, in that it's down to the lex/parse (eg > a=' omg spaces!'; b='oh Noes! '$a; echo "$b" # works fine -- but note > what happens when you echo $b with no quotes.) kojiro knows more about the > corner cases where quoting is needed in [[, but it _never_ shields you from > quoting issues outside tests, and you *will* be told about it if you paste > an unsafe loc in #bash. (We don't care if that's not the current problem as > you see it: if you're not quoting right, your scripts are worse than > useless, since they'll break when your users least expect it, for no > apparent reason, and you're nowhere to be found. People soon learn not to > paste unsafe expansions ;p and they get into the habit of always quoting.) > > [1] USE MORE QUOTES! Read > http://bash-hackers.org/wiki/doku.php?id=syntax:words to understand why. > http://www.grymoire.com/Unix/Quote.html will tell you how. -- Optimally, > you should quote every parameter expansion ($foo). So in other words, [ ] always needs quotes whereas [[ ]] normally doesn't but could need quotes in corner cases. I know which I'd prefer - certainty over uncertainty. Thanks Roy -- [EMAIL PROTECTED] mailing list