Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance SC2178/SC2128 errors when the variable is an internal shell array variable #1101

Open
3 of 4 tasks
mat813 opened this issue Jan 24, 2018 · 1 comment
Open
3 of 4 tasks

Comments

@mat813
Copy link

mat813 commented Jan 24, 2018

For bugs

  • Rule Id (if any, e.g. SC1000): SC2178/SC2128
  • My shellcheck version (shellcheck --version or "online"): online
  • I tried on shellcheck.net and verified that this is still a problem on the latest commit
  • It's not reproducible on shellcheck.net, but I think that's because it's an OS, configuration or encoding issue

For new checks and feature suggestions

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

#!/bin/sh

GROUPS=$1

if [ -n "${GROUPS}" ]; then
  echo "${GROUPS}";
fi

Here's what shellcheck currently says:

Line 3:
GROUPS=$1
^-- SC2178: Variable was used as an array but is now assigned a string.
 
Line 5:
if [ -n "${GROUPS}" ]; then
         ^-- SC2128: Expanding an array without an index only gives the first element.
 
Line 6:
  echo "${GROUPS}";
        ^-- SC2128: Expanding an array without an index only gives the first element.

Here's what I wanted or expected to see:

Something telling me GROUPS is an internal shell variable that is an array, which is why it's telling me this.

It took me a very long time to see that if I renamed the variable, then it stopped badgering me about it, which I was very, very puzzled about. Mostly because FreeBSD's sh, which is the one I am working with, does not have arrays at all.
it took me even longer, mostly by accident, to find out that while FreeBSD's sh did not have an internal variable named GROUPS, bash had one, and it was probably why I was getting the error.

@kapsh
Copy link

kapsh commented Jan 20, 2019

I also have a worst test-case with $GROUPS which puzzled me recently. Shellcheck doesn't warn about reusing builtin variables leading to completely wrong work.

❯ cat groups.sh 
#!/bin/bash

GROUPS=( hurr durr )
for g in "${GROUPS[@]}"; do
    echo "$g"
done

/tmp
❯ shellcheck groups.sh; echo $?
0

/tmp
❯ bash groups.sh 
2000
6
10

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants