问题描述
我正在尝试编写一个脚本,该脚本将使用 echo 并写入/附加到文件.但是我已经在字符串中的语法中有"..说..
I am trying to write a script which will use echo and write/append to a file.But I have " " in syntax already in strings .. say ..
echo "I am "Finding" difficult to write this to file" > file.txt
echo "I can "write" without double quotes" >> file.txt
任何人都可以帮助理解这一点,非常感谢.
Can anyone please help to understand this, really appreciated.
BR,SM
推荐答案
如果你想有引号,那么你必须使用反斜杠字符转义它们.
If you want to have quotes, then you must escape them using the backslash character.
echo "I am \"Finding\" difficult to write this to file" > file.txt echo
echo "I can \"write\" without double quotes" >> file.txt
如果您也想自己编写 \
也是如此,因为它可能会导致副作用.所以你必须使用 \\
The same holds true if you i.e. also want to write the \
itself, as it may cause side effects. So you have to use \\
另一种选择是使用 `'' 而不是引号.
Another option would be to use The `'' instead of quotes.
echo 'I am "Finding" difficult to write this to file' > file.txt echo
echo 'I can "write" without double quotes' >> file.txt
但是在这种情况下变量替换不起作用,所以如果你想使用变量,你必须把它们放在外面.
However in this case variable substition doesn't work, so if you want to use variables you have to put them outside.
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
这篇关于如何使用 echo 命令写入和附加到文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!