From 05448b2e5c0e8e79b44e3572b4b5f47ea42c8b95 Mon Sep 17 00:00:00 2001 From: koalaman Date: Sat, 8 Feb 2014 15:10:40 -0800 Subject: [PATCH] Created SC2028 (markdown) --- SC2028.md | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 SC2028.md diff --git a/SC2028.md b/SC2028.md new file mode 100644 index 0000000..86ecc37 --- /dev/null +++ b/SC2028.md @@ -0,0 +1,25 @@ +# echo won't expand escape sequences. Consider printf. + +### Problematic code: + + echo "Name:\t$value" + +### Correct code: + + printf "Name:\t%s\n" "$value" + +### Rationale: + +Backslash escapes like `\t` and `\n` are not expanded by echo, and become literal backslash-t, backslash-n. + +`printf` does expand these sequences, and should be used instead. + +Other, non-portable methods include `echo -e '\t'` and `echo $'\t'`. ShellCheck will warn if this is used in a script with shebang `#!/bin/sh`. + +If you actually wanted a literal backslash-t, use + + echo "\\t" + +### Contraindications + +None \ No newline at end of file