From c417e96fa468c516c49b5d4e02b3f8503d75f2e1 Mon Sep 17 00:00:00 2001 From: Vidar Holen Date: Sun, 21 Oct 2018 15:27:09 -0700 Subject: [PATCH] Created SC2049 (markdown) --- SC2049.md | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 SC2049.md diff --git a/SC2049.md b/SC2049.md new file mode 100644 index 0000000..e61491e --- /dev/null +++ b/SC2049.md @@ -0,0 +1,37 @@ +## =~ is for regex, but this looks like a glob. Use = instead. + +### Problematic code: + +```sh +[[ $file =~ *.txt ]] +``` + +### Correct code: + +```sh +[[ $file = *.txt ]] +``` + +### Rationale: + +You are using `=~` to match against a regex -- specifically a Extended Regular Expression (ERE) -- but the right-hand side looks more like a glob: + +* It may have a leading `*`, like in `*.txt` + * In a glob, this matches strings ending in `.txt`, like `readme.txt` but not `foo.sh` + * In an ERE, this matches a literal asterisk, followed by any character, and then `txt`, such as `*itxt` but not `test.txt` + +* It may be a single letter followed by a `*`, like in `s*`. + * In a glob, this matches strings starting with `s`, such as `shell` and `set`. + * In an ERE, this matches zero or more `s`s, such as `dog` (because it does in fact contain zero or more `s`'s) + +Please ensure that the pattern is correct as an ERE, or switch to glob matching if that's what you intended. + +This is similar to [[SC2063]], where `grep "*foo*"` produces an equivalent warning. + +### Exceptions: + +If you are aware of the difference, you can [[ignore]] this message, but this warning is not emitted for the more probable EREs `\*.txt`, `\.txt$`, `^s` or `s+`, so it should rarely be necessary. + +### Related resources: + +* Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc! \ No newline at end of file