58

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

2 Answers 2

76

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 "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 '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
5
  • Thanks for your help . Earlier I forgot to told about single quote. Yes, I tried single quote and that worked for me but not fulfill the purpose . Trying using escape backslash character .. Thanks, SM Commented Jun 19, 2013 at 10:56
  • Thanks Guys, Using Backslash it is done now ,, and also fulfill my purpose . You guys just awesome . Regards, SM Commented Jun 19, 2013 at 11:11
  • 1
    it doesn't work for me! 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
    – xgqfrms
    Commented Dec 17, 2016 at 2:02
  • why is there a second echo at the end of the first example? I think this is not needed.
    – Sebastian
    Commented Oct 18, 2023 at 9:40
  • @Sebastian I sure dont know how this got there. In the editing history its not there. I removed it now.
    – Devolus
    Commented Oct 19, 2023 at 5:24
36

If you have special characters, you can escape them with a backslash to use them as needed:

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

However, you can also use the shell's "EOF" feature with the tee command, which is really nice for writing all sorts of things:

tee -a file.txt <<EOF

I am "Finding" difficult to write this to file
I can "write" without double quotes
EOF

That will write virtually ANY content you want directly to that file, and escape any special characters until you get to the EOF.

*Edited to add the append switch, to prevent overwriting the file:
-a

4
  • Great answer. I improved the answer but I wasnt logged in. It was the addition of the -a switch to prevent overwrite.
    – semtex41
    Commented Feb 13, 2019 at 3:33
  • tee just saved me a lot of manual shell time...looking to turn this into an ansible playbook with some additional vars to scale it out, thanks!
    – ficestat
    Commented May 17, 2022 at 15:10
  • 2
    nice. but it would have been useful to tell the difference between > file.txt and >> file.txt: > file.txt: overwrites the context of the file with the new text >> file.txt: pushes the new text to existing context
    – Cristea
    Commented Feb 2, 2023 at 15:26
  • Good solution. From what I can see though, you cannot have commands interspersed within the EOF to EOF block. You may want to write a line, execute a command, then write another line and so on.
    – Chiwda
    Commented Jan 21 at 3:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.