Dagang Wei
Dagang Wei

Reputation: 26488

How to preserve newline characters in grep result?

I want to assign the grep result to a variable for further use:

lines=$(cat abc.txt | grep "hello")

but seems newline characters are removed in the result, when I do

echo $lines

only one line is printed. How can I preserve newline characters, so when I echo $lines, it generates the same result as cat abc.txt | grep "hello" does.

Upvotes: 37

Views: 24932

Answers (2)

Haggisbreath
Haggisbreath

Reputation: 41

Using the Windows port for grep (not the original question, I kow and not applicable to *nix). I found that -U solved the problem nicely.

From the --help:

-U, --binary do not strip CR characters at EOL (MSDOS)

Upvotes: 2

Sean
Sean

Reputation: 29772

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.

Upvotes: 66

Related Questions