gpt4 book ai didi

bash - 在 heredoc 中对 shell 变量使用双引号

转载 作者:行者123 更新时间:2023-11-29 09:37:32 27 4
gpt4 key购买 nike

我编写了一个 bash 脚本并通过读取获得了一些用户输入。我想把我通过阅读获得的变量放在一个文件中 cat << EOF >> file .我的问题是每个变量都有“双引号”。我怎样才能避免这种情况?

echo "Whats your name?"
read yourname

cat << EOF >> /path/to/file
Your Name is "${yourname}"

EOF

文件内容为:

Your Name is "djgudb"

应该是:

Your Name is djgudb

最佳答案

引号在 heredocs 中没有句法意义,所以如果您不希望它们是字面意思,请不要将它们放在那里。

echo "Whats your name?"
read yourname

cat << EOF >> /path/to/file
Your Name is ${yourname}

EOF

关于bash - 在 heredoc 中对 shell 变量使用双引号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47436478/

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