相关疑难解决方法(0)

\n在heredoc变量中

有没有办法让Bash heredoc在heredoc中解释'\n \'?

我在循环中有一个迭代构建的字符串,类似于

for i in word1 word2 word3
do
        TMP_VAR=$i
        ret="$ret\n$TMP_VAR"
done
Run Code Online (Sandbox Code Playgroud)

然后我想在heredoc中使用创建的字符串:

cat <<EOF > myfile
HEADER
==
$ret
==
TRAILER
EOF
Run Code Online (Sandbox Code Playgroud)

但是我想将"\n"字符解释为换行符,以便输出为

HEADER
==
word1
word2
word3
==
TRAILER
Run Code Online (Sandbox Code Playgroud)

代替

HEADER
==
\nword1\nword2\nword3
==
TRAILER
Run Code Online (Sandbox Code Playgroud)

可能吗?或者我应该以某种方式构建我的初始字符串?

variables bash escaping heredoc

9
推荐指数
4
解决办法
7701
查看次数

标签 统计

bash ×1

escaping ×1

heredoc ×1

variables ×1