diff --git a/SC2307.md b/SC2307.md new file mode 100644 index 0000000..e89c198 --- /dev/null +++ b/SC2307.md @@ -0,0 +1,42 @@ +## 'expr' expects 3+ arguments but sees 1. Make sure each operator/operand is a separate argument, and escape <>&|. + +### Problematic code: + +```sh +# | not escaped +expr 1 | 2 +# > not escaped +expr "$foo" >= "$bar" + +# Missing spaces around + +expr 1+2 +# Unexpected quoting around an expression +expr "1 + 2" +``` + +### Correct code: + +```sh +expr 16 \| 7 +expr "$foo" \>= "$bar" +expr 1 + 2 +``` + +### Rationale: + +ShellCheck found an `expr` command with 1 or 2 arguments. `expr` normally expects 3 or more. + +Generally, this happens for one of two reasons: + +* You are using an operator like `|`, `&`, `>`, `>=`, `<`, `<=`, which needs to be escaped to avoid the shell interpreting it as a pipe, backgrounded command, or redirection. +* You don't have spaces around operators and operands (or have bad quotes) which causes them not to be separate arguments. + +Make sure each operator or operand to `expr` is a separate argument, and that anything containing shell metacharacters is escaped. The correct code shows examples of each. + +### Exceptions: + +None + +### Related resources: + +* Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc! \ No newline at end of file