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

SC2068 and array indices #1787

Open
2 tasks done
scop opened this issue Jan 4, 2020 · 0 comments
Open
2 tasks done

SC2068 and array indices #1787

scop opened this issue Jan 4, 2020 · 0 comments

Comments

@scop
Copy link
Contributor

scop commented Jan 4, 2020

For bugs

  • Rule Id (if any, e.g. SC1000): SC2068
  • My shellcheck version (shellcheck --version or "online"): online
  • The rule's wiki page does not already cover this (e.g. https://shellcheck.net/wiki/SC2086)
  • I tried on 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
a=(a b c)
echo ${!a[@]}

Here's what shellcheck currently says:

Line 3:
echo ${!a[@]}
     ^-- SC2068: Double quote array expansions to avoid re-splitting elements.

Here's what I wanted or expected to see:

${!a[@]} (with the exclamation mark) expands to array indices, so for normal arrays there's no danger of re-splitting. However, if the array is an associative array, then the indices can contain whitespace and the warning is good.

Improving this depends on how deeply shellcheck understands the code:

  • if it knows it's dealing with a usual non-associative array, it could omit the warning, and keep emitting it for associative arrays
  • if it doesn't know the above, the check can be left as is and the limitation should be documented in the Wiki

(Haven't checked if it's already there, but a unit test would be good to have to ensure desired behavior even if it's the current one.)

scop added a commit to scop/bash-completion that referenced this issue Jan 4, 2020
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

1 participant