bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: shell variable references - coding style


From: Pavel Raiskup
Subject: Re: shell variable references - coding style
Date: Wed, 13 Mar 2019 13:46:29 +0100

On Tuesday, February 19, 2019 7:02:08 PM CET Bruno Haible wrote:
> Hi Pavel,
> 
> > > [...]
> > > This patch fixes both issues, and makes the IFS handling a bit more 
> > > robust.
> > > [...]
> > 
> > > -    case $_fpf_arg in
> > > +    case "$_fpf_arg" in
> > > [...]
> > > -  fpf_dirs=$1 ; shift
> > > -  fpf_cb=$1 ; shift
> > > +  fpf_dirs="$1"; shift
> > > +  fpf_cb="$1"; shift
> > > [...]
> > 
> > ... and so on, I don't think it is more robust.  At least according to
> > Autoconf's Shellology [1] it should be actually better to write it the
> > other way around (without additional quotes).
> 
> I do think it is more robust, because
>   * The number one mistake in shell scripts (measured by frequency of
>     occurrence) is to reference variables without double quotes when word
>     splitting is in fact undesired.
>   * Simple rules are easier to follow by programmers, resulting in fewer bugs.
>   * The rule
>       "Always double-quote shell variable references, except if you DO
>        want word-splitting."
>     is simpler than
>       "Always double-quote shell variable references, except if you DO
>        want word-splitting OR in the right-hand side of assignments OR
>        as argument of 'case' statements."
> 
> The text that you quote says two different things:
>   * Backquotes inside double-quotes are hairy.
>     There is a simple rule to avoid them: When you have a backquote
>     expression, always first assign its result to a variable. Then use
>     the variable (with double-quotes, usually).
>   * Bash 4.1 has a bug when you WANT word splitting.
> These two things don't make my style rule
>   "Always double-quote shell variable references, except if you DO
>    want word-splitting."
> less robust.
> 
> > FTR, Gary Vaughan has wrote a syntax checker rules for protecting us from
> > adding such statements into libtool codebase.
> 
> Opinions regarding coding style differ. Gary is entitled to his opinion, as
> much as I am entitled to mine.

Is there a gnulib/GNU-preferred way to do this, defined somewhere?  I'd
like to avoid such mistakes in future so the code I add doesn't need an
unnecessary rewrites (such style changes only complicate git-log reading,
and hide the real gist of the changes).

Pavel


> Bruno
> 
> > [1] https://www.gnu.org/software/autoconf/manual/autoconf-2.69/html_node/
> > Shell-Substitutions.html
> 
> 
> 







reply via email to

[Prev in Thread] Current Thread [Next in Thread]