mirror of
https://github.com/koalaman/shellcheck.git
synced 2025-10-03 19:29:44 +08:00
Created SC2053 (markdown)
24
SC2053.md
Normal file
24
SC2053.md
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
## Quote the rhs of = in [[ ]] to prevent glob matching.
|
||||||
|
|
||||||
|
### Problematic code:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
[[ $a = $b ]]
|
||||||
|
```
|
||||||
|
|
||||||
|
### Correct code:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
[[ $a = "$b" ]]
|
||||||
|
```
|
||||||
|
### Rationale:
|
||||||
|
|
||||||
|
When the right-hand side of `=`, `==` or `!=` is unquoted in `[[ .. ]]`, it will be treated like a glob.
|
||||||
|
|
||||||
|
This has some unexpected consequences like `[[ $var = $var ]]` being false (for `var='[a]'`), or `[[ $foo = $bar ]]` giving a different result from `[[ $bar = $foo ]]`.
|
||||||
|
|
||||||
|
The most common intention is to compare one variable to another as strings, in which case the right-hand side must be quoted.
|
||||||
|
|
||||||
|
### Exceptions:
|
||||||
|
|
||||||
|
If you explicitly want to match against a pattern, you can [[ignore]] this warning.
|
Reference in New Issue
Block a user