gpt4 book ai didi

shell - 如何使用 echo 命令写入和附加到文件

转载 作者:行者123 更新时间:2023-12-03 14:41:31 37 4
gpt4 key购买 nike

我正在尝试编写一个脚本,该脚本将使用 echo 并写入/附加到文件。
但是我已经在字符串中使用了“”语法..说..

echo "I am "Finding" difficult to write this to file" > file.txt
echo "I can "write" without double quotes" >> file.txt

任何人都可以帮助理解这一点,真的很感激。

BR,
SM

最佳答案

如果你想有引号,那么你必须使用反斜杠字符来转义它们。

echo "I am \"Finding\" difficult to write this to file" > file.txt echo
echo "I can \"write\" without double quotes" >> file.txt

如果您也想写 \,同样如此。本身,因为它可能会引起副作用。所以你必须使用 \\
另一种选择是使用 `'' 而不是引号。
echo 'I am "Finding" difficult to write this to file' > file.txt echo
echo 'I can "write" without double quotes' >> file.txt

但是在这种情况下变量替换不起作用,所以如果你想使用变量,你必须把它们放在外面。
echo "This is a test to write $PATH in my file" >> file.txt
echo 'This is a test to write '"$PATH"' in my file' >> file.txt

关于shell - 如何使用 echo 命令写入和附加到文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17189237/

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