gpt4 book ai didi

bash - 猫不在文件中写入特殊变量

转载 作者:行者123 更新时间:2023-11-29 09:38:25 24 4
gpt4 key购买 nike

我正在使用 cat 通过脚本 shell 写入文件。

 cat <<EOF >/home/test.txt
set $value1 $value2;
EOF

我的文件只包含这个字符串“set ;”但我需要找到“set $value1 $value2;”。我猜 cat 认为“$value1”是一个 shell 变量,但它只是一个我需要写入文件的字符串。

最佳答案

在 EOF 周围使用引号:

cat <<"EOF" >/home/test.txt
set $value1 $value2;
EOF

来自man bashHere Documents部分:

The format of here-documents is:

   <<[-]word
here-document
delimiter

If any characters in word are 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.

关于bash - 猫不在文件中写入特殊变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18636761/

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