当echo命令将数据存储在变量中时,我正在尝试检索换行符。有人能帮帮我吗?
当我运行echo -e时,它给出了命令行中预期的输出
bash>testfile='INFO\nERROR\nWARNING'
bash> echo -e "$testfile"`
INFO
ERROR
WARNING但是,当我尝试将输出存储在变量中时,我缺少换行符。
bash>testoutput=`echo -e "$testfile" | grep 'WARNING\|ERROR'`
bash>echo $testoutput
ERROR WARNING感谢您的建议
发布于 2019-12-29 02:18:00
在unix stackexchange上查看this answer。
看起来换行符就在那里,echo只是出于某种原因不想打印它们。相反,请查看printf。
brundage@fatty ~ $ testfile='INFO\nERROR\nWARNING'
brundage@fatty ~ $ echo -e "$testfile"
INFO
ERROR
WARNING
brundage@fatty ~ $ testoutput=`echo -e "$testfile" | grep 'WARNING\|ERROR'`
brundage@fatty ~ $ printf "$testoutput\n"
ERROR
WARNINGhttps://stackoverflow.com/questions/59513392
复制相似问题