将cat输出保存到变量中时,所有换行符都将被删除 [英] All newlines are removed when saving cat output into a variable

查看:59
本文介绍了将cat输出保存到变量中时,所有换行符都将被删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下文件

linux$ cat test.txt
toto
titi
tete
tata

将cat输出保存到变量中将删除换行符

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?

推荐答案

shell正在拆分 msgs 变量,以便 echo 获得多个参数.您需要引用变量以防止这种情况发生:

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"

这篇关于将cat输出保存到变量中时,所有换行符都将被删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆