test.t-6ren">
gpt4 book ai didi

linux - 将数组元素传递给 Heredoc

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:50:24 26 4
gpt4 key购买 nike

我正在尝试将一个数组元素传递给 heredoc,目标是生成一个文件,例如:

declare -a box=("element1" "element2" "element3")

cat > test.txt <<-EOF
some text, insert first element
some text, insert second element
some text, insert third element
EOF

这可能吗?我该如何实现?

最佳答案

您可以使用 $(..) 嵌套循环:

declare -a box=("element1" "element2" "element3")

cat > test.txt <<-EOF
Greetings,

Here are the elements you wanted:
$(
for s in "${box[@]}"
do
echo "some text, $s"
done
)

Happy New Year from $USER
EOF

执行时,会生成一个 test.txt,其中包含:

Greetings,

Here are the elements you wanted:
some text, element1
some text, element2
some text, element3

Happy New Year from myusername

关于linux - 将数组元素传递给 Heredoc,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54029951/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com