shellcheck icon indicating copy to clipboard operation
shellcheck copied to clipboard

SC2086 piping edge case

Open IvanGrigorik opened this issue 6 months ago • 2 comments

For bugs with existing features

  • Rule Id: SC2086
  • My shellcheck version: online
  • [x] The rule's wiki page does not already cover this
  • [x] I tried on https://www.shellcheck.net/ and verified that this is still a problem on the latest commit

Here's a snippet or screenshot that shows the problem:

#!/bin/bash

for module in $(find "./" -maxdepth 1 -name "*.txt" | sort); do
    echo "$module"
done

Here's what shellcheck currently says:

Nothing

Here's what I wanted or expected to see:

for module in $(find "./" -maxdepth 1 -name "*.txt" | sort); do
              ^-- SC2044 (warning): For loops over find output are fragile. Use find -exec or a while read loop.

Rationale:

In this case, piping should not affect the check, since the execution process is still invalid for files with spaces.

IvanGrigorik avatar Jul 30 '25 20:07 IvanGrigorik

Well, for myself, I would agree that this execution process (eg, the command substitution in the example) is problematic for files with spaces. For this for loop in particular, with echo "$module", word splitting probably wouldn't result in a shell error. ...although the data produced could be to some extent inaccurate.

So this bug report's subject references SC2086 (Double quote to prevent globbing and word splitting), but the body references SC2044 (For loops over find output are fragile.)

Which shellcheck rule were you wanting to see?

wileyhy avatar Jul 31 '25 16:07 wileyhy

I would like to see SC2044 here, as using globbing instead of the find command would resolve the issue.

IvanGrigorik avatar Aug 04 '25 19:08 IvanGrigorik