我有这个多行字符串(包括引号):
abc'asdf"
$(dont-execute-this)
foo"bar"''
如何在 Bash 中使用 heredoc 将其分配给变量?
我需要保留换行符。
我不想转义字符串中的字符,那会很烦人...
您可以通过以下方式避免无用的使用cat
和更好地处理不匹配的引号:
$ read -r -d '' VAR <<'EOF'
abc'asdf"
$(dont-execute-this)
foo"bar"''
EOF
如果您在回显变量时不引用该变量,则会丢失换行符。引用它可以保留它们:
$ echo "$VAR"
abc'asdf"
$(dont-execute-this)
foo"bar"''
如果您想在源代码中使用缩进来提高可读性,请在小于号后使用破折号。缩进必须仅使用制表符(无空格)来完成。
$ read -r -d '' VAR <<-'EOF'
abc'asdf"
$(dont-execute-this)
foo"bar"''
EOF
$ echo "$VAR"
abc'asdf"
$(dont-execute-this)
foo"bar"''
相反,如果您想在结果变量的内容中保留制表符,则需要从IFS
. 此处文档 ( EOF
) 的终端标记不得缩进。
$ IFS='' read -r -d '' VAR <<'EOF'
abc'asdf"
$(dont-execute-this)
foo"bar"''
EOF
$ echo "$VAR"
abc'asdf"
$(dont-execute-this)
foo"bar"''
可以通过按Ctrl-在命令行中插入选项卡V Tab。如果您使用的是编辑器,取决于哪个编辑器,它可能也可以工作,或者您可能必须关闭自动将制表符转换为空格的功能。
使用 $() 将输出分配cat
给您的变量,如下所示:
VAR=$(cat <<'END_HEREDOC'
abc'asdf"
$(dont-execute-this)
foo"bar"''
END_HEREDOC
)
# this will echo variable with new lines intact
echo "$VAR"
# this will echo variable without new lines (changed to space character)
echo $VAR
确保用单引号分隔起始 END_HEREDOC。
请注意,结尾的 heredoc 分隔符END_HEREDOC
必须单独在行上(因此结尾括号在下一行)。
感谢您@ephemient
的回答。
这是丹尼斯方法的变体,在脚本中看起来更优雅。
函数定义:
define(){ IFS='\n' read -r -d '' ${1} || true; }
用法:
define VAR <<'EOF'
abc'asdf"
$(dont-execute-this)
foo"bar"''
EOF
echo "$VAR"
请享用
ps为不支持read -d
. 应该与set -eu
未配对的反引号一起使用,但没有经过很好的测试:
define(){ o=; while IFS="\n" read -r a; do o="$o$a"'
'; done; eval "$1=\$o"; }
VAR=<<END
abc
END
不起作用,因为您将标准输入重定向到不关心它的东西,即分配
export A=`cat <<END
sdfsdf
sdfsdf
sdfsfds
END
` ; echo $A
有效,但那里有一个回击可能会阻止你使用它。此外,您应该避免使用反引号,最好使用命令替换表示法$(..)
。
export A=$(cat <<END
sdfsdf
sdfsdf
sdfsfds
END
) ; echo $A
仍然没有保留换行符的解决方案。
这不是真的——你可能只是被 echo 的行为误导了:
echo $VAR # strips newlines
echo "$VAR" # preserves newlines
分支尼尔的答案,您通常根本不需要 var ,您可以以与变量几乎相同的方式使用函数,并且它比内联或read
基于 - 的解决方案更容易阅读。
$ complex_message() {
cat <<'EOF'
abc'asdf"
$(dont-execute-this)
foo"bar"''
EOF
}
$ echo "This is a $(complex_message)"
This is a abc'asdf"
$(dont-execute-this)
foo"bar"''
数组是一个变量,因此在这种情况下 mapfile 将起作用
mapfile y <<'z'
abc'asdf"
$(dont-execute-this)
foo"bar"''
z
然后你可以像这样打印
printf %s "${y[@]}"
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
)"
感谢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
#!/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
我发现自己必须读取一个包含 NULL 的字符串,所以这里有一个解决方案,可以读取你扔给它的任何东西。尽管如果您实际上正在处理 NULL,您将需要在十六进制级别处理它。
$ cat > read.dd.sh
read.dd() {
buf=
while read; do
buf+=$REPLY
done < <( dd bs=1 2>/dev/null | xxd -p )
printf -v REPLY '%b' $( sed 's/../ \\\x&/g' <<< $buf )
}
证明:
$ . read.dd.sh
$ read.dd < read.dd.sh
$ echo -n "$REPLY" > read.dd.sh.copy
$ diff read.dd.sh read.dd.sh.copy || echo "File are different"
$
HEREDOC 示例(带有 ^J、^M、^I):
$ read.dd <<'HEREDOC'
> (TAB)
> (SPACES)
(^J)^M(^M)
> DONE
>
> HEREDOC
$ declare -p REPLY
declare -- REPLY=" (TAB)
(SPACES)
(^M)
DONE
"
$ declare -p REPLY | xxd
0000000: 6465 636c 6172 6520 2d2d 2052 4550 4c59 declare -- REPLY
0000010: 3d22 0928 5441 4229 0a20 2020 2020 2028 =".(TAB). (
0000020: 5350 4143 4553 290a 285e 4a29 0d28 5e4d SPACES).(^J).(^M
0000030: 290a 444f 4e45 0a0a 220a ).DONE
$TEST="ok"
read MYTEXT <<EOT
this bash trick
should preserve
newlines $TEST
long live perl
EOT
echo -e $MYTEXT