我怎么preserve换行符在bash引用字符串? [英] how do I preserve newlines in a quoted string in bash?
问题描述
我创建一个脚本来自动的Apache虚拟主机的创建。我的脚本的一部分是这样的:
I'm creating a script to automate the creation of apache virtual hosts. Part of my script goes like this:
MYSTRING="<VirtualHost *:80>
ServerName $NEWVHOST
DocumentRoot /var/www/hosts/$NEWVHOST
...
"
echo $MYSTRING
不过,在脚本中的换行符被忽略。如果我呼应的字符串,是被吐出来为一行。
However, the line breaks in the script are being ignored. If I echo the string, is gets spat out as one line.
我怎么能保证换行打印?
How can I ensure that the line breaks are printed ?
推荐答案
添加引号,使其工作:
echo "$MYSTRING"
看它是这样的:
MYSTRING="line-1
line-2
line3"
echo $MYSTRING
这将作为执行:
echo line-1 \
line-2 \
line-3
即。 回声
有三个参数,打印使用空间的每个参数在他们之间。
i.e. echo
with three parameters, printing each parameter with a space in between them.
如果您周围添加 $ myString的
报价,得到的命令是:
If you add quotes around $MYSTRING
, the resulting command will be:
echo "line-1
line-2
line-3"
即。 回声
与有三行文字和两个换行一个字符串参数。
i.e. echo
with a single string parameter which has three lines of text and two line breaks.
这篇关于我怎么preserve换行符在bash引用字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!