shellcheck
shellcheck copied to clipboard
Usage Error for (( x = 1 )) vs ( x == 1 ))
For new checks and feature suggestions
- [x] 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 the problem:
#!/bin/bash
# Incorrect code with accidental assignment
if (( row = 1 )); then
# Corrected code for equality test
if (( row == 1 )); then
~~~~~~~~~~~~~~
row=2; (( row = 1 )); echo $?; echo $row
# 0
# 1
row=2; (( row == 1 )); echo $?; echo $row
# 1
# 2
Here's what shellcheck currently says:
No issues detected!
Here's what I wanted or expected to see:
With spaces I expected the single "=" to act as an equality comparison like it does with "[" or "[[" for string literals but with "((" this apparently still works as an assignment. Of course this is wrong but was an easy mistake to make and took me a little while to figure out what was wrong. It'd be nice if SC flagged this a probable usage error.
This is my first enhancement request. Think it's legit but either way shellcheck is a FANTASTIC resource. Thanks for creating it!!