mirror of
https://github.com/koalaman/shellcheck.git
synced 2025-10-03 11:19:45 +08:00
Created SC1076 (markdown)
26
SC1076.md
Normal file
26
SC1076.md
Normal file
@@ -0,0 +1,26 @@
|
||||
## Trying to do math? Use e.g. `[ $((i/2+7)) -ge 18 ]`.
|
||||
|
||||
### Problematic code:
|
||||
|
||||
```sh
|
||||
[ i / 2 + 7 -ge 18 ]
|
||||
```
|
||||
|
||||
### Correct code:
|
||||
|
||||
```sh
|
||||
[ $((i / 2 + 7)) -ge 18 ]
|
||||
```
|
||||
### Rationale:
|
||||
|
||||
ShellCheck found a loose `+*/%` in a test statement. This usually happens when trying to do arithmetic in a condition, but without using the arithmetic expansion construct `$((expression))`.
|
||||
|
||||
In C, `if (a+b == c)` is perfectly fine, but in sh this must be written to first expand the arithmetic operation like `if [ $((a+b)) = c ]`.
|
||||
|
||||
### Exceptions:
|
||||
|
||||
None.
|
||||
|
||||
### Related resources:
|
||||
|
||||
* Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc!
|
Reference in New Issue
Block a user