Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to append several lines of text in a file using a shell script

I want to write several lines (5 or more) to a file I'm going to create in script. I can do this by echo >> filename. But I would like to know what the best way to do this?

like image 828
Yrgl Avatar asked Feb 14 '11 08:02

Yrgl


People also ask

How do you append multiple lines in Unix?

Method # 1 – Using echo & Printf The simplest way to append multiple lines to a file is to use the echo and printf command. Let us start with echo. Echo is a command used to output a string or multiple strings as arguments.

How do you write a multi line shell script?

In Shell or Bash shell, we can comment on multiple lines using << and name of comment. we start a comment block with << and name anything to the block and wherever we want to stop the comment, we will simply type the name of the comment.

How do you append to a file in shell?

Append Text Using >> Operator The >> operator redirects output to a file, if the file doesn't exist, it is created but if it exists, the output will be appended at the end of the file. For example, you can use the echo command to append the text to the end of the file as shown.

How can I append some lines in existing file using which command?

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.


1 Answers

You can use a here document:

cat <<EOF >> outputfile some lines of text EOF 
like image 136
Dennis Williamson Avatar answered Sep 24 '22 09:09

Dennis Williamson