shellcheck icon indicating copy to clipboard operation
shellcheck copied to clipboard

Add special bash variables to detection + warn if using "incorrectly"

Open DreadPirateShawn opened this issue 4 months ago • 0 comments

For new checks and feature suggestions

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

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

Script:

#!/usr/bin/env bash

readarray -t LINES <<EOF
foo
bar
EOF

echo "# Array is correct:"
for x in "${LINES[@]}"; do
  echo "- $x"
done

echo "# Array is (still) correct:"
for x in "${LINES[@]}"; do
  echo "- $x"
done

echo "# Invoke grep"
grep --version &>/dev/null

echo "# Array is now modified:"
for x in "${LINES[@]}"; do
  echo "- $x"
done

Output:

$ ./demo.sh 
# Array is correct:
- foo
- bar
# Array is (still) correct:
- foo
- bar
# Invoke grep
# Array is now modified:
- 37
- bar

Here's what shellcheck currently says:

$ shellcheck myscript
No issues detected!

Here's what I wanted to see:

Something along the lines of:

The LINES variable is a special Bash Shell variable: https://www.gnu.org/savannah-checkouts/gnu/bash/manual/bash.html#index-LINES This is used by the underlying Bash system to indicate the column length for printing selection lists. Setting this value directly should be performed with caution, as it may be changed by various other activity (such as the grep binary).

DreadPirateShawn avatar Aug 18 '25 22:08 DreadPirateShawn