From c3af8beb21e2d9d5e5520cde1ca5fe731c036808 Mon Sep 17 00:00:00 2001 From: koalaman Date: Sat, 18 Jul 2015 12:43:09 -0700 Subject: [PATCH] Created SC2162 (markdown) --- SC2162.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 SC2162.md diff --git a/SC2162.md b/SC2162.md new file mode 100644 index 0000000..ce35476 --- /dev/null +++ b/SC2162.md @@ -0,0 +1,23 @@ +## read without -r mangles backslashes + +### Problematic code: + + echo "Enter name:" + read name + +### Correct code: + + echo "Enter name:" + read -r name + +### Rationale: + +By default, `read` will interpret backslashes before spaces and line feeds, and otherwise strip them. This is rarely expected or desired. + +Normally you just want to read data, which is what `read -r` does. All `read`s should use `-r` unless you have a good reason not to. + +Note that `read -r` will still strip leading and trailing spaces. `IFS="" read -r` prevents this. + +### Exceptions: + +If you want backslashes to affect field splitting and line terminators instead of being read, you can disable this message with a [[directive]]. \ No newline at end of file