sdb*_*bbs 3 bash newline string-concatenation
我已经看到在 bash 中连接两个字符串变量附加换行符- 但当我读到它时,解决方案是:
像这样用双引号回显:
...但我似乎无法重现它 - 这是一个例子:
$ bash --version
GNU bash, version 5.0.17(1)-release (x86_64-pc-linux-gnu)
$ mystr=""
$ mystr="${mystr}First line here\n"
$ mystr="${mystr}Second line here\n"
$ mystr="${mystr}Third line here\n"
$ echo $mystr
First line here\nSecond line here\nThird line here\n
Run Code Online (Sandbox Code Playgroud)
到目前为止,正如预期的那样 - 这是双引号:
$ echo "$mystr"
First line here\nSecond line here\nThird line here\n
Run Code Online (Sandbox Code Playgroud)
我再次没有得到新的行 - 所以建议“用双引号回显它”似乎没有正确。
任何人都可以准确地说\n,在连接字符串时如何获得正确的换行符输出(而不仅仅是) bash?
在字符串中添加一个换行符,而不是两个字符\和,。n
mystr=""
mystr+="First line here"$'\n'
mystr+="Second line here"$'\n'
mystr+="Third line here"$'\n'
echo "$mystr"
Run Code Online (Sandbox Code Playgroud)
或者您可以解释\转义序列 - with sed、 withecho -e或 with printf "%b" "$mystr"。