用换行符在bash中构建字符串 [英] Build a string in bash with newlines
本文介绍了用换行符在bash中构建字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在遍历bash中的脚本,具体取决于要附加到变量的条件,然后在最后显示它,如下所示:
I'm going through a script in bash where depending on the conditionals I want to append to a variable different things and then display it at the very end, something like this:
VAR="The "
if [[ whatever ]]; then
VAR="$VAR cat wears a mask"
elif [[ whatevs ]]; then
VAR="$VAR rat has a flask"
fi
但是,如果我偶尔想在其中添加换行符,则尝试通过附加这种形式来建立VAR的方式而遇到麻烦.例如,我该怎么做VAR="$VAR\nin a box"
?我以前曾见过$'\n'
的用法,但由于附加而在尝试使用$VAR
时也未见.
but i run into difficulties if I try to use this form of building up VAR by appending to it when I want to occasionally append newlines into it. How would I do VAR="$VAR\nin a box"
, for example? I have seen usage of $'\n'
before but not when also trying to use $VAR
because of the appending.
推荐答案
查看全文