Pregunta

I have the following file

linux$ cat test.txt
toto
titi
tete
tata

Saving the cat output into a variable will discard the newlines

linux$ msgs=`cat test.txt`
linux$ echo $msgs
toto titi tete tata

How to keep the output containing the newlines in the variables?

¿Fue útil?

Solución

The shell is splitting the msgs variable so echo get multiple parameters. You need to quote your variable to prevent this to happen:

echo "$msgs"

Otros consejos

I had this:

echo $(cat myfile.sh) >> destination.sh

that was causing the problem, so I changed it to:

cat myfile.sh >> destination.sh

and that worked, lulz

you can use the redirection "|"

echo | cat test.txt
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top