#!/usr/local/bin/bash
out=`grep apache README`
echo $out;
Usually grep shows each match on a separate line when run on the command line. However, in the above scripts, the newline separating each match disappears. Does anyone know how the newline can be preserved?
You're not losing it in the assignment but in the echo. You can see this clearly if you:
You'll see a similar effect with the following script:
which outputs:
And, irrelevant to your question but I'd like to mention it anyway, I prefer to use the $() construct rather than backticks, just for the added benefit of being able to nest commands. So your script line becomes:
Now that may not look any different (and it isn't) but it makes possible more complex commands like:
Put $out in quotes:
Quoting variables in bash preserves the whitespace.
For instance:
since newlines are whitespace they get "removed"