From 57551b5b9f7e8c7d18385944148c0af4271b9fcb Mon Sep 17 00:00:00 2001 From: koalaman Date: Mon, 9 Nov 2015 10:46:13 -0800 Subject: [PATCH] Created SC2021 (markdown) --- SC2021.md | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 SC2021.md diff --git a/SC2021.md b/SC2021.md new file mode 100644 index 0000000..68f0f1b --- /dev/null +++ b/SC2021.md @@ -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=]`. \ No newline at end of file