From f0f12de728bdf67fe61c7ace7935e68ecf3aaf78 Mon Sep 17 00:00:00 2001 From: Vidar Holen Date: Sun, 30 Jun 2019 19:04:39 -0700 Subject: [PATCH] Created SC1135 (markdown) --- SC1135.md | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 SC1135.md diff --git a/SC1135.md b/SC1135.md new file mode 100644 index 0000000..827b3b7 --- /dev/null +++ b/SC1135.md @@ -0,0 +1,30 @@ +## Prefer escape over ending quote to make `$` literal. Instead of `"It costs $"5`, use `"It costs \$5"` + +### Problematic code: + +```sh +echo "The apples are $""1 each" +eval "var=$"name +``` + +### Correct code: + +```sh +echo "The apples are \$1 each" +eval "var=\$name" +# or better yet: var="${!name}" +``` + +### Rationale: + +The script appears to be closing a double quoted string for the sole purpose of making a dollar sign `$` literal. + +While this happens to work, the better solution is instead to escape it with a backslash. This allows the double quoted string to continue uninterrupted, thereby reducing the visual noise of stopping and starting quotes in the middle of a shell word. + +### Exceptions: + +None + +### Related resources: + +* Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc! \ No newline at end of file