The difference between the quoted and unquoted versions may depend on your shell, but most likely is:
- quoted,
"$foo"
represents a single argument which is passed to the echo
command, which prints it verbatim, followed by a newline.
- unquoted,
$foo
expands to a set of arguments separated by $IFS
. If $IFS
is unset, it defaults to whitespace, and if there is no field separator, you have just one field.
In the unquoted version, there's no reason to expect that the newline will be considered a part of the output, as it isn't even really part of the input to the echo
command. It simply separates arguments.
If you want to change this behaviour, you could change your $IFS
setting, but I don't recommend it. Change IFS when you need to process fields, not to format your output.
For predictable output, follow the POSIX.1 recommendation, and don't use echo
at all. Use printf
instead.