문제

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?

도움이 되었습니까?

해결책

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"

다른 팁

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
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top