[bash] Open and write data to text file using Bash?

How can I write data to a text file automatically by shell scripting in Linux?

I was able to open the file. However, I don't know how to write data to it.

This question is related to bash shell

The answer is

The short answer:

echo "some data for the file" >> fileName

However, echo doesn't deal with end of line characters (EOFs) in an ideal way. So, if you're gonna append more than one line, do it with printf:

printf "some data for the file\nAnd a new line" >> fileName

The >> and > operators are very useful for redirecting output of commands, they work with multiple other bash commands.

Similar questions with bash tag:

Similar questions with shell tag: