From 9bae18f627ceba1107aac3fee915bf23e760f443 Mon Sep 17 00:00:00 2001 From: koalaman Date: Mon, 26 Jun 2017 17:34:46 -0700 Subject: [PATCH] Created SC2074 (markdown) --- SC2074.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 SC2074.md diff --git a/SC2074.md b/SC2074.md new file mode 100644 index 0000000..c1ec533 --- /dev/null +++ b/SC2074.md @@ -0,0 +1,22 @@ +## Can't use `=~` in `[ ]`. Use `[[..]]` instead. + +### Problematic code: + +```sh +[ "$input" =~ DOC[0-9]*\.txt ] && echo "match" +``` + +### Correct code: + +```sh +[[ "$input" =~ DOC[0-9]*\.txt ]] && echo "match" +``` +### Rationale: + +`=~` only works in `[[ .. ]]` tests. It does not work with `test` or `[` in any shell. + +If you're targeting POSIX `sh`, rewrite in terms of `case` or `grep` instead. + +### Exceptions: + +None. \ No newline at end of file