From 10021b79d3adba333238455c6a748eb2ff8a1d4a Mon Sep 17 00:00:00 2001 From: koalaman Date: Mon, 11 Jul 2016 00:05:50 -0500 Subject: [PATCH] Created SC2081 (markdown) --- SC2081.md | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 SC2081.md diff --git a/SC2081.md b/SC2081.md new file mode 100644 index 0000000..10902d4 --- /dev/null +++ b/SC2081.md @@ -0,0 +1,30 @@ +## `[ .. ]` can't match globs. Use `[[ .. ]]` or grep. + +### Problematic code: + +```sh +if [ $var == *[^0-9]* ] +then + echo "$var is not numeric" +fi +``` + +### Correct code: + +```sh +if [[ $var == *[^0-9]* ]] +then + echo "$var is not numeric" +fi +``` +### Rationale: + +`[ .. ]` aka `test` can not match against globs. + +In bash/ksh, you can instead use `[[ .. ]]` which supports this behavior. + +In sh, you can rewrite to use `grep`. + +### Exceptions: + +None. If you are not trying to match a glob, quote the argument (e.g. `[ $var == '*' ]` to match literal asterisk. \ No newline at end of file