-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

According to Dave Korn on 8/14/2009 7:11 AM:
>> a="`""`"
>>
>> is non-portable, but can always be replaced by the portable:
>>
>> a=`""`
>>
>> See why I don't like spurious "" around variable assignments?
> 
>   Ah, now I do.  Ok, but is some nice clear list of where you should and
> shouldn't quote anywhere?  As far as I can tell you have to memorize the
> entire bash manual and then infer what to do from all the overlapping sets of
> expansions and contexts and rules.  Then you have to to the same for all the
> other shells we're supposed to support.  Then you have to infer a set of
> lowest-common-denominator rules from all of that.

I think it's as simple as:

if you are doing variable assignment, there is no word splitting, so you
only need quotes if it would otherwise look like multiple words:

a=$1
b="$1 $2"

if you are doing case statements, there is no word splitting, so you only
need quotes if it would otherwise look like multiple words:

case $1 in
case " $1 " in

if you are doing `` which needs embedded quoting, then it is only safe to
do it as variable assignment (where `` looks like a single word, without
needing outer ""):

a=`echo "$1 $2"`

pretty much every where else, all $ expansions need double quotes if you
want to avoid word splitting (but using $ expansions to hide tool names is
one case where you DO want word splitting):

ECHO='printf %s\n'
$ECHO "$1"

And beyond that, it's just lots of practice.  I agree that it often feels
like you have to memorize loads of information to write portable shell,
but at least these public reviews catch a lot of the pitfalls.

- --
Don't work too hard, make some time for fun as well!

Eric Blake             e...@byu.net
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (Cygwin)
Comment: Public key at home.comcast.net/~ericblake/eblake.gpg
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAkqFYVAACgkQ84KuGfSFAYBIRwCgxRt0p2lnFnSfQOHl0TR1puou
rPoAoJsTLkaB4QMg6ZcTsD2bKqNWftfQ
=wagC
-----END PGP SIGNATURE-----


Reply via email to