Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I write and append using echo command to a file

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

like image 370
user2500742 Avatar asked Jun 19 '13 10:06

user2500742


People also ask

How do I append a line in echo?

This appending task can be done by using 'echo' and 'tee' commands. Using '>>' with 'echo' command appends a line to a file. Another way is to use 'echo,' pipe(|), and 'tee' commands to add content to a file.

Which command is used to append in a file?

How do I use the cat command to append data to a file? You can use the cat command to append data or text to a file. The cat command can also append binary data. The main purpose of the cat command is to display data on screen (stdout) or concatenate files under Linux or Unix like operating systems.

How do you append data to a file in Linux?

The >> operator redirects output to a file. If the mentioned file doesn't exist the file is created and then the text is appended to the file. Alternatively, we can use printf command to append text into a file. We can also use the cat command to append the content of one file to another file.


2 Answers

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 
like image 144
Devolus Avatar answered Oct 19 '22 14:10

Devolus


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

like image 23
JoeLinux Avatar answered Oct 19 '22 15:10

JoeLinux