igor igor - 1 year ago 66
Bash Question

Escaping rules when reading in bash

Assume the file

is created as the following:

> cat > text

The following two scripts generate different outputs and I don't understand why.

> s=$(cat text)
> echo $s
> cat text | while read line; do echo $line; done


Answer Source

read without the -r option interprets backslash escapes, and \a becomes a. See the manual:

If this option is given, backslash does not act as an escape character. The backslash is considered to be part of the line. In particular, a backslash-newline pair may not be used as a line continuation.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download