mirror of
https://github.com/koalaman/shellcheck.git
synced 2025-10-03 19:29:44 +08:00
Created SC1026 (markdown)
30
SC1026.md
Normal file
30
SC1026.md
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
## If grouping expressions inside [[..]], use ( .. ).
|
||||||
|
|
||||||
|
### Problematic code:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
[[ [ a || b ] && c ]]
|
||||||
|
[ [ a -o b ] -a c ]]
|
||||||
|
```
|
||||||
|
|
||||||
|
### Correct code:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
[[ ( a || b ) && c ]]
|
||||||
|
[ \( a -o b \) -a c ]] # or { [ a ] || [ b ]; } && [ c ]
|
||||||
|
```
|
||||||
|
### Rationale:
|
||||||
|
|
||||||
|
`[ .. ]` should not be used to group subexpressions inside `[[ .. ]]` or `[ .. ]` statements.
|
||||||
|
|
||||||
|
For `[[ .. ]]`, use regular parentheses.
|
||||||
|
|
||||||
|
For `[ .. ]`, either use escaped parentheses, or preferably rewrite the expression into multiple `[ .. ]` joined with `&&`, `||` and `{ ..; }` groups.
|
||||||
|
|
||||||
|
### Exceptions:
|
||||||
|
|
||||||
|
None
|
||||||
|
|
||||||
|
### Related resources:
|
||||||
|
|
||||||
|
* [Bash Pitfalls: `if [ [ a = b ] && [ c = d ] ]; then ...`](https://mywiki.wooledge.org/BashPitfalls#pf11)
|
Reference in New Issue
Block a user