From fba52fef9ce2dd6aaaaf2dd247ce60c17935b5b6 Mon Sep 17 00:00:00 2001 From: koalaman Date: Sat, 10 May 2014 14:23:10 -0700 Subject: [PATCH] Created SC1083 (markdown) --- SC1083.md | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 SC1083.md diff --git a/SC1083.md b/SC1083.md new file mode 100644 index 0000000..f74cbd4 --- /dev/null +++ b/SC1083.md @@ -0,0 +1,28 @@ +## This } is literal. Check expression (missing ;/\n?) or quote it. + +### Problematic code: + + rmf() { rm -f "$@" } + + eval echo \${foo} + +### Correct code: + + rmf() { rm -f "$@"; } + + eval "echo \${foo}" +### Rationale: + +Curly brackets are normally used as syntax in parameter expansion, command grouping and brace expansion. + +However, if they don't appear alone at the start of an expression or as part of a parameter or brace expansion, the shell silently treats them as literals. This frequently indicates a bug, so ShellCheck warns about it. + +In the example function, the `}` is literal because it's not at the start of an expression. We fix it by adding a `;` before it. + +In the example eval, the code works fine. However, we can quiet the warning and follow good practice by adding quotes around the literal data. + +ShellCheck does not warn about `{}`, since this is frequently used with `find` and rarely indicates a bug. + +### Contraindications + +This error is harmless when the curly brackets are supposed to be literal, in e.g. `awk {'print $1'}`. However, it's cleaner and less error prone to simply include them inside the quotes: `awk '{print $1}'`. \ No newline at end of file