Open and write data to text file using Bash?

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.

Leave a Comment