Ubuntu – Why does bash remove \n in $(cat file)

bashcommand line

If I have the file input.txt containing:


Then executing the bash command echo $(cat input.txt) will output this:

hello world !

Why and how can I fix it to output exactly what is in the file how it is in the file?

Best Answer

If you use

echo "$(cat input.txt)"

it will work correctly.

Probably the input of echo is separated by newlines, and it will handle it as separate commands, so the result will be without newlines.