mirror of
https://github.com/koalaman/shellcheck.git
synced 2025-10-03 19:29:44 +08:00
Created SC2306 (markdown)
35
SC2306.md
Normal file
35
SC2306.md
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
## Escape glob characters in arguments to expr to avoid pathname expansion.
|
||||||
|
|
||||||
|
### Problematic code:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
f=$(expr "$c" * 9 / 5 + 32)
|
||||||
|
```
|
||||||
|
|
||||||
|
### Correct code:
|
||||||
|
|
||||||
|
Prefer rewriting to a modern style (see [[SC2003]]):
|
||||||
|
|
||||||
|
```sh
|
||||||
|
f=$((c * 9 / 5 + 32))
|
||||||
|
```
|
||||||
|
|
||||||
|
If you do not wish to do so, at least escape the glob characters when passing them to `expr`:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
f=$(expr "$c" \* 9 / 5 + 32)
|
||||||
|
```
|
||||||
|
|
||||||
|
### Rationale:
|
||||||
|
|
||||||
|
`expr` is a command so `expr 2 * 2` will consider `*` to mean "all files in the current directory". This causes the expression to fail to evaluate unless you are in an empty directory with the `failglob` and `nullglob` options turned off.
|
||||||
|
|
||||||
|
Prefer rewriting it using the modern, POSIX standard arithmetic expansion `$((..))`. If you do not wish to do so, you can escape any characters like `*` to avoid the shell performing pathname expansion on them.
|
||||||
|
|
||||||
|
### Exceptions:
|
||||||
|
|
||||||
|
None
|
||||||
|
|
||||||
|
### Related resources:
|
||||||
|
|
||||||
|
* Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc!
|
Reference in New Issue
Block a user