我编写了一个bash脚本,并通过read获取了一些用户输入。
我想通过读取将变量放在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
答案 0 :(得分:5)
引号在heredocs中没有句法意义,所以如果你不希望它们是文字的话,不要把它们放在那里。
echo "Whats your name?"
read yourname
cat << EOF >> /path/to/file
Your Name is ${yourname}
EOF
答案 1 :(得分:2)
bash手册说:
The format of here-documents is:
[n]<<[-]word
here-document
delimiter
No parameter and variable expansion, command substitution, arithmetic
expansion, or pathname expansion is performed on word. If any part of
word is quoted, the delimiter is the result of quote removal on word,
and the lines in the here-document are not expanded. If word is
unquoted, all lines of the here-document are subjected to parameter
expansion, command substitution, and arithmetic expansion, the charac‐
ter sequence \<newline> is ignored, and \ must be used to quote the
characters \, $, and `.
所以你需要
cat << EOF >> /path/to/file
Your Name is ${yourname}
EOF