shellcheck icon indicating copy to clipboard operation
shellcheck copied to clipboard

It's not possible to ignore rules on just the first line of the script due to broken parsing of file-wide ignores.

Open lietu opened this issue 5 years ago • 3 comments

You've changed it so that the whole file ignore does apparently NOT need to immediately follow the shebang, which leads to broken logic.

#!/usr/bin/env sh
# shellcheck disable=SC2039
set -exuo pipefail

and

#!/usr/bin/env sh

# shellcheck disable=SC2039
set -exuo pipefail

Produce the same results, when obviously the expectation is different.

The first variant should ignore SC2039 for the whole file, the second variant should ignore it only on the set -exuo pipefail -line.

You also cannot move the # shellcheck disable=SC2039 to the end of the relevant line as you would expect from all other linters, as that just leads to other complaints.

When the instruction to enable checks again was requested, now invalid information was given about how this works: https://github.com/koalaman/shellcheck/issues/755

It was claimed you could fix this by adding some comment or command:

#!/usr/bin/env sh
# This line is important
# shellcheck disable=SC2039
set -exuo pipefail

You cannot.

What DOES work as a workaround is:


#!/usr/bin/env sh
: # This line is important
# shellcheck disable=SC2039
set -exuo pipefail

The : is apparently the same as true, and gets parsed as "some command that is basically always ok and cannot cause warnings", and then the # shellcheck disable= will be parsed correctly to apply to just the one line.

For bugs

  • My shellcheck version (shellcheck --version or "online"): 0.7.0
  • [x] The rule's wiki page does not already cover this (e.g. https://shellcheck.net/wiki/SC2086)
  • [x] I tried on shellcheck.net and verified that this is still a problem on the latest commit

For new checks and feature suggestions

  • [ ] shellcheck.net (i.e. the latest commit) currently gives no useful warnings about this
  • [ ] I searched through https://github.com/koalaman/shellcheck/issues and didn't find anything related

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

#!/usr/bin/env sh

# shellcheck disable=SC2039
set -exuo pipefail

if [[ "${ENV}" == "foo" ]]; then
  echo "bar"
fi

Here's what shellcheck currently says:

No issues detected!

Here's what I wanted or expected to see:

In test.sh line 6:
if [[ "${ENV}" == "foo" ]]; then
   ^---------------------^ SC2039: In POSIX sh, [[ ]] is undefined.

lietu avatar Mar 18 '20 14:03 lietu

I just found the ignore option yesterday and quickly discovered the noop solution for this. Seems like a reasonable solution for this edge case but should be a documented feature of the ignore functionality.

iconoclasthero avatar Nov 12 '25 14:11 iconoclasthero

+1 I have just discovered this annoying pitfall... When I disable a lint for the 1st command the disabling applies to the whole file. This behavior makes it easy to get false negatives without a warning. Yes, workarounds do exist, but they are all ugly and require me to remember about them. Ideally, there should be a distinct file-wide shellcheck disable syntax. At least there should be a command line switch to treat shellcheck disables on top of the file as applied to the 1st line only.

YuriyGulyayev avatar Nov 17 '25 20:11 YuriyGulyayev

Annotations would definitely have been designed differently today 😅

koalaman avatar Nov 17 '25 21:11 koalaman