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

SC2199 warns on [[ ${ARRAY[@]:x:1} == ... ]] #885

Closed
1 task done
floatingatoll opened this issue Apr 21, 2017 · 3 comments
Closed
1 task done

SC2199 warns on [[ ${ARRAY[@]:x:1} == ... ]] #885

floatingatoll opened this issue Apr 21, 2017 · 3 comments

Comments

@floatingatoll
Copy link

floatingatoll commented Apr 21, 2017

For bugs

  • Rule Id: SC2199
  • My shellcheck version: online
  • 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=(1 2 3); if [[ ${A[@]:0:1} == '1' ]]; then true; fi

Here's what shellcheck currently says:

SC2199: Arrays implicitly concatenate in [[ ]]. Use a loop (or explicit * instead of @).

Here's what I wanted or expected to see:

No warning, since the array is sliced to a single element before implicit concatenation occurs.

@koalaman
Copy link
Owner

You're right in this particular case because the array is statically defined, but in general this is not the case: it will expand to 1 or 0 elements.

Here's what happens in [ which doesn't implicitly concatenate:

$ A=(); [ "${A[@]:0:1}" == "foo" ]  && echo ok
bash: [: ==: unary operator expected

If you want 0 elements to be considered an empty string, you should explicitly concatenate the 0 or 1 elements so that you always end up with a string to compare. Alternatively, you can use "${A[0]}" which is shorter and amounts to the same.

@floatingatoll
Copy link
Author

floatingatoll commented Apr 23, 2017 via email

@floatingatoll
Copy link
Author

(I suppose there's an opportunity for ❌1 warning specifically recommending the simpler syntax.)

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