我有这个多行字符串(包括引号):
abc'asdf"
$(dont-execute-this)
foo"bar"''
我将如何分配它到一个变量使用heredoc在Bash?
我需要保留换行符。
我不想转义字符串中的字符,这将是恼人的…
我有这个多行字符串(包括引号):
abc'asdf"
$(dont-execute-this)
foo"bar"''
我将如何分配它到一个变量使用heredoc在Bash?
我需要保留换行符。
我不想转义字符串中的字符,这将是恼人的…
当前回答
这是Dennis方法的变化,在脚本中看起来更优雅。
函数定义:
define(){ IFS='\n' read -r -d '' ${1} || true; }
用法:
define VAR <<'EOF'
abc'asdf"
$(dont-execute-this)
foo"bar"''
EOF
echo "$VAR"
享受
附注:为不支持read -d的shell创建了一个“read循环”版本。应该适用于set -eu和未配对的反引号,但测试不太好:
define(){ o=; while IFS="\n" read -r a; do o="$o$a"'
'; done; eval "$1=\$o"; }
其他回答
仍然没有保留换行符的解决方案。
这不是真的——你可能只是被echo的行为误导了:
返回$VAR #换行符
echo "$VAR" #保留换行符
将heredoc值赋给变量
VAR="$(cat <<'VAREOF'
abc'asdf"
$(dont-execute-this)
foo"bar"''
VAREOF
)"
用作命令的参数
echo "$(cat <<'SQLEOF'
xxx''xxx'xxx'xx 123123 123123
abc'asdf"
$(dont-execute-this)
foo"bar"''
SQLEOF
)"
$TEST="ok"
read MYTEXT <<EOT
this bash trick
should preserve
newlines $TEST
long live perl
EOT
echo -e $MYTEXT
数组是一个变量,所以在这种情况下mapfile可以工作
mapfile y <<'z'
abc'asdf"
$(dont-execute-this)
foo"bar"''
z
然后你可以像这样打印
printf %s "${y[@]}"
感谢dimo414的回答,这展示了他的伟大解决方案是如何工作的,并表明你可以在文本中轻松地使用引号和变量:
示例输出
$ ./test.sh
The text from the example function is:
Welcome dev: Would you "like" to know how many 'files' there are in /tmp?
There are " 38" files in /tmp, according to the "wc" command
test.sh
#!/bin/bash
function text1()
{
COUNT=$(\ls /tmp | wc -l)
cat <<EOF
$1 Would you "like" to know how many 'files' there are in /tmp?
There are "$COUNT" files in /tmp, according to the "wc" command
EOF
}
function main()
{
OUT=$(text1 "Welcome dev:")
echo "The text from the example function is: $OUT"
}
main