mirror of
https://github.com/koalaman/shellcheck.git
synced 2025-10-03 19:29:44 +08:00
Created SC2021 (markdown)
24
SC2021.md
Normal file
24
SC2021.md
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
## Don't use [] around ranges in tr, it replaces literal square brackets.
|
||||||
|
|
||||||
|
### Problematic code:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
tr -cd '[a-z]'
|
||||||
|
```
|
||||||
|
|
||||||
|
### Correct code:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
tr -cd 'a-z'
|
||||||
|
```
|
||||||
|
### Rationale:
|
||||||
|
|
||||||
|
Ancient System V `tr` required brackets around operands, but modern implementations including POSIX, GNU, OS X and *BSD instead treat them as literals.
|
||||||
|
|
||||||
|
Unless you want to operate on literal square brackets, don't include them.
|
||||||
|
|
||||||
|
### Exceptions:
|
||||||
|
|
||||||
|
If you do want to replace literal square brackets, reorder the expression (e.g. `a-z[]` to make it clear that the brackets are not special).
|
||||||
|
|
||||||
|
ShellCheck does not warn about correct usage of `[..]` in character and equivalence classes like `[:lower:]` and `[=e=]`.
|
Reference in New Issue
Block a user