In Linux, the “echo” command is utilized to print the strings or lines of text passed as an argument. To put these strings or lines of text into a file, the redirection operator “>” is considered. Sometimes the user may need to put each string or line into a new file line to give better formatting. For that purpose, the special character “\n” is examined.
This post will demonstrate how to put a new line using a special character into a file with a combination of echo and redirection operators.
- Through the “echo” Command and Redirection Operator
- Create New Lines into a File (New File)
- Create New Lines into a File (Existing File)
How to Put a New Line Into a File? echo and Redirection Operator
To insert a new line into a file through the “echo” command and redirection operator, the following syntax is considered:
Syntax:
$ echo -e "[String 1] \n[String 2]...\n[String n] > [File_name](File Does not Exist)$ echo -e "[Line 1] \n[Line 2] .....\n[Line n] >> [File_name] (File Exist)
The description of the above syntax is given below:
- The “echo” command is used to print lines.
- The “e” flag to enables the backslash escapes.
- Write the “Line 1”, “Line 2,” and “Line n” separated by the special character “\n” to put each string to the new lines.
- The redirection operator “>” is used to create the new file or the “>>” operator if the file already exists.
Example 1: Create New Lines into a File (New File)
An example is considered for printing the lines “Welcome” to itslinuxfoss.” and “Henry Here.” to a new line using the special character “\n.” Then the output is forwarded to the “New_file.txt” using the redirection operator “>”:
$ echo -e "Welcome to itslinuxfoss. \n Henry Here." > New_file.txt
The file “New_file.txt” is created with the above-given strings.
Let’s verify the content of the “New_file.txt” using the cat command:
$ cat New_file.txt
Each string is printed in the new line, as shown in the above figure.
Example 2: Create New Lines into a File (Existing File)
Another example is carried out to append data into an existing file using the redirection operator “>>”. In our case, the file “file.txt” is available with the following content:
$ cat file.txt
The content of the “file.txt” is displayed in the terminal.
The echo command prints the strings “This is Line 1.”, “This is Line 2.” and “This is line 3” to a new line using the special character “\n.” Then the output is forwarded to the “file.txt” using the “>>” operator and append to the file:
$ echo -e "This is Line 1. \nThis is Line 2. \nThis is line 3" >> file.txt
The successful execution of command shows that strings are appended in the file.
Let’s check the output of the “file.txt”:
$ cat file.txt
The given strings are appended in a file that can be seen in the above image.
Conclusion
To put a new line into a file using the echo command and redirection operator, use the “e” flag to enable backslash escapes. Then separate each string by a special character “\n”. If the user wants to create a new file, then utilize the “echo -e “[String 1] \n[String 2]…\n[String n] > [File_name]” syntax. To append lines in an already existing file, utilize the “echo -e “[String 1] \n[String 2]…\n[String n] >> [File_name]” syntax.
This write-up has illuminated the examples of putting a new line into a file by using the special character and echo command with the redirection operator.