On Sun, Mar 17, 2024 at 09:25:10AM +0000, Tim Woodall wrote:
> I have this one-liner (which works but shellcheck doesn't like the
> quoting)
> 
> idxsrc="$( newest_file $( APT_CONFIG=${APT_CONFIG} apt-get indextargets 
> --format '$(FILENAME)' 'Identifier: Packages' ))"
> 
> SC2016: Expressions don't expand in single quotes, use double quotes for that.
> SC2046: Quote this to prevent word splitting.

I can't say that I blame it.  I don't like that quoting either.

However, the thing that *it* doesn't like isn't the same as the thing
that *I* don't like.

It's complaining about '$(FILENAME)' which is a questionable design
choice by the authors of apt-get.  Not much to be done about that.

I'd complain about  newest_file $(cmd)  instead.  It looks like you're
running a command which returns a bunch of filenames, and you want to
select one of the names from this bunch.  Right?

This only works when none of the filenames contain any whitespace or
globbing characters.  Otherwise, there's no way to know which sets of
characters constitute a single filename.  Is "a b c d" one filename,
or two filenames, or three, or four?  There's no way to tell.

> The first is easy enough to avoid by using backslash instead. But the
> second I can't see how to fix as a one-liner.

Oh, I see.  It actually *did* complain about the same thing that I
complained about.  I didn't understand at first that these were two
separate warnings.

> I can make shellcheck happy by doing it like this:
> 
> mapfile -t idxpackages < <( APT_CONFIG=${APT_CONFIG} apt-get indextargets 
> --format \$\(FILENAME\) 'Identifier: Packages' )
> idxsrc="$( newest_file "${idxpackages[@]}")"

Ah, so apt-get returns one filename per line, and you're splitting the
stream on newlines?  I suppose that works well enough in practice, for
the output of apt-get.  It's not too likely that any of your packages
will contain filenames with newlines in them.

In any case, this is *definitely* better than your first command, which
splits on *all* whitespace, not just newlines, and additionally performs
globbing.  If we ignore the possibility of filenames with newlines,
then this fixes all of the issues I can see.

> I have a number of other places where I'm relying on a variable
> containing a number of space separated items that I DO want word
> splitting and so the shellcheck warning is incorrect and I either
> suppress it or find a fix similar to the above.

All such cases fail when one of your "words" may contain whitespace
or globbing characters.

> In almost all other cases, the space separated items cannot, even in
> theory, contain a rogue space, so suppressing the warning is fine

Famous Last Words™.

Reply via email to