On Fri, 25 Apr 2025 at 22:44, Lee <ler...@gmail.com> wrote:
> On Fri, Apr 25, 2025 at 12:51 PM Greg Wooledge wrote:
> > On Fri, Apr 25, 2025 at 11:33:54 -0400, Lee wrote:

> > > > Also, you should quote "$tempf".

[...]

> > But why take the chance?

> You're right - I should be working on the habit of putting quotes
> around filename vars.  Always.

> I wanted to know if what I did was wrong or just not as correct as it
> could be.  So in this case, it seems like not perfect but not wrong
> either :)

Hi, seeing as you're asking about good shell script practice, I'll share
what I have found to be the "perfect" approach for me:

1) Understand the different forms of quoting and what they do
   (I think that's the part that feels like most effort).

2) Always quote, except when you absolutely need something to be unquoted
   (I don't find this to be much effort).

This approach causes double-quotes to signify to me that "this variable
expansion is normal, nothing unusual", as well as the other features they
provide.

And seeing single-quotes or backslash tells me that there is no variable
expansion occurring.

The main benefit I get from this approach is then that in the rare occasion
that I have to use an unquoted variable in my own code, seeing the lack of
quotes alerts me explicitly that I must need "something out of the
ordinary" to happen in that expansion (usually splitting whitespace or
expanding a glob). Example:

$ f='*'
$ echo "$f"
*
$ echo '$f'
$f
$ echo \$f
$f
$ echo $f
every filename in current directory

Reply via email to