How to preserve newline characters in grep result?

You want to say

echo "$lines"

instead of

echo $lines

To elaborate:

echo $lines means “Form a new command by replacing $lines with the contents of the variable named lines, splitting it up on whitespace to form zero or more new arguments to the echo command. For example:

lines="1 2 3"
echo $lines   # equivalent to "echo 1 2 3"
lines="1   2   3"
echo $lines   # also equivalent to "echo 1 2 3"
lines="1
2
3"
echo $lines   # also equivalent to "echo 1 2 3"

All these examples are equivalent, because the shell ignores the specific kind of whitespace between the individual words stored in the variable lines. Actually, to be more precise, the shell splits the contents of the variable on the characters of the special IFS (Internal Field Separator) variable, which defaults (at least on my version of bash) to the three characters space, tab, and newline.

echo "$lines", on the other hand, means to form a single new argument from the exact value of the variable lines.

For more details, see the “Expansion” and “Word Splitting” sections of the bash manual page.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)