452

我有这个多行字符串(包括引号):

abc'asdf"
$(dont-execute-this)
foo"bar"''

如何在 Bash 中使用 heredoc 将其分配给变量?

我需要保留换行符。

我不想转义字符串中的字符,那会很烦人...

4

11 回答 11

608

您可以通过以下方式避免无用的使用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。如果您使用的是编辑器,取决于哪个编辑器,它可能也可以工作,或者您可能必须关闭自动将制表符转换为空格的功能。

于 2009-10-31T19:41:21.270 回答
331

使用 $() 将输出分配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的回答。

于 2009-07-22T20:37:07.123 回答
91

这是丹尼斯方法的变体,在脚本中看起来更优雅。

函数定义:

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"; }
于 2011-11-11T00:12:40.897 回答
49
VAR=<<END
abc
END

不起作用,因为您将标准输入重定向到不关心它的东西,即分配

export A=`cat <<END
sdfsdf
sdfsdf
sdfsfds
END
` ; echo $A

有效,但那里有一个回击可能会阻止你使用它。此外,您应该避免使用反引号,最好使用命令替换表示法$(..)

export A=$(cat <<END
sdfsdf
sdfsdf
sdfsfds
END
) ; echo $A
于 2009-07-22T19:57:50.150 回答
43

仍然没有保留换行符的解决方案。

这不是真的——你可能只是被 echo 的行为误导了:

echo $VAR # strips newlines

echo "$VAR" # preserves newlines

于 2013-08-08T12:58:55.873 回答
18

分支尼尔的答案,您通常根本不需要 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"''
于 2016-01-27T22:10:45.240 回答
14

数组是一个变量,因此在这种情况下 mapfile 将起作用

mapfile y <<'z'
abc'asdf"
$(dont-execute-this)
foo"bar"''
z

然后你可以像这样打印

printf %s "${y[@]}"
于 2014-12-25T19:30:44.297 回答
3

将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
)"
于 2013-07-23T05:20:38.893 回答
2

感谢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

测试.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
于 2016-02-10T17:31:53.743 回答
1

我发现自己必须读取一个包含 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
于 2012-06-04T04:41:29.047 回答
-10
$TEST="ok"
read MYTEXT <<EOT
this bash trick
should preserve
newlines $TEST
long live perl
EOT
echo -e $MYTEXT
于 2009-08-27T15:10:05.063 回答