我有这个多行字符串(包括引号):
abc'asdf"
$(dont-execute-this)
foo"bar"''
Run Code Online (Sandbox Code Playgroud)
如何在Bash中使用heredoc将其分配给变量?
我需要保留换行符.
我不想逃避字符串中的字符,这会很烦人......
我找到了一些办法通过外壳变量的awk脚本,但我感到困惑'和".
首先,我尝试使用shell脚本:
$ v=123test
$ echo $v
123test
$ echo "$v"
123test
Run Code Online (Sandbox Code Playgroud)
然后尝试了awk:
$ awk 'BEGIN{print "'$v'"}'
$ 123test
$ awk 'BEGIN{print '"$v"'}'
$ 123
Run Code Online (Sandbox Code Playgroud)
为什么有区别?
最后我试过这个:
$ awk 'BEGIN{print " '$v' "}'
$ 123test
$ awk 'BEGIN{print ' "$v" '}'
awk: cmd. line:1: BEGIN{print
awk: cmd. line:1: ^ unexpected newline or end of string
Run Code Online (Sandbox Code Playgroud)
我对此感到困惑.