[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: shell variable references - coding style
From: |
Jim Meyering |
Subject: |
Re: shell variable references - coding style |
Date: |
Tue, 19 Feb 2019 11:41:27 -0800 |
On Tue, Feb 19, 2019 at 10:02 AM Bruno Haible <address@hidden> 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.
>
> Bruno
>
> > [1] https://www.gnu.org/software/autoconf/manual/autoconf-2.69/html_node/
> > Shell-Substitutions.html
I too prefer to avoid double quotes after assignment-"=": less syntax
feels slightly more maintainable, here.
These days I rely heavily on shellcheck. I find it to be very useful,
and at least by default it doesn't complain about such usage.