Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open and write data to text file using Bash?

Tags:

bash

shell

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.

like image 317
Adnan Avatar asked Jun 22 '12 18:06

Adnan


People also ask

How do you write data into a text file in Unix?

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 I write to a text file in Linux?

In Linux, to write text to a file, use the > and >> redirection operators or the tee command.


1 Answers

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.

like image 64
Rocky Pulley Avatar answered Sep 22 '22 00:09

Rocky Pulley