From 5f3998712d2e22ca4c7425e3612d6bde175ffc53 Mon Sep 17 00:00:00 2001 From: Vidar Holen Date: Sun, 22 Aug 2021 20:14:10 -0700 Subject: [PATCH] Created SC2305 (markdown) --- SC2305.md | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 SC2305.md diff --git a/SC2305.md b/SC2305.md new file mode 100644 index 0000000..46655ce --- /dev/null +++ b/SC2305.md @@ -0,0 +1,29 @@ +## Quote regex argument to expr to avoid it expanding as a glob. + +### Problematic code: + +```sh +expr "$input" : [0-9]* +``` + +### Correct code: + +```sh +expr "$input" : "[0-9]*" +``` + +### Rationale: + +ShellCheck found an `expr` command using `:` to match a regex, but the regex is not quoted and therefore being treated as a glob. + +This means that if the problematic code is ever executed in a directory containing a file matching `[0-9]*`, such as `2021-reports` or `12 Angry Men [1957].mkv`, it will be replaced be replaced and cause the command to error or incorrectly match. + +The regex should be quoted to avoid this, like in the correct example. + +### Exceptions: + +None + +### Related resources: + +* Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc! \ No newline at end of file