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.
You want to say
instead of
To elaborate:
echo $linesmeans “Form a new command by replacing$lineswith the contents of the variable namedlines, splitting it up on whitespace to form zero or more new arguments to theechocommand. For example: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 specialIFS(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 variablelines.For more details, see the “Expansion” and “Word Splitting” sections of the bash manual page.