-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Comments
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
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. |
ooo.
Thank you! Sorry to bother.
… On Apr 22, 2017, at 19:33, koalaman ***@***.***> wrote:
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.
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub, or mute the thread.
|
(I suppose there's an opportunity for ❌1 warning specifically recommending the simpler syntax.) |
For bugs
Here's a snippet or screenshot that shows the problem:
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.
The text was updated successfully, but these errors were encountered: