Print Newline in Bash

# Print Newline in Bash

### Introduction

In Bash, a new line refers to the end of a line of text and the beginning of a new one. When a command is executed in Bash, the output is often displayed on the terminal with each line of text ending in a new line. The new line character is represented by a special escape sequence, \n, which tells the terminal to move the cursor to the beginning of the next line.

Printing a new line in Bash is important because it helps to format the output in a way that is easy to read and understand. For example, if you are printing the results of a command that produces a lot of output, using new lines can make it easier to parse the information. Additionally, new lines can be used to visually separate different sections of output, making it easier to locate specific information.

In this article, we'll take a a look at several different ways to print a new line, including using the echo command with the -e option, the printf command, and the escape sequence \n.

### Two Ways to Print a New Line in Bash

Here are several different ways to print a new line in Bash. In this section we'll take a look at two most common.

#### Using the echo command with the "-e" option

The echo command is used to display a message or output on the Bash. It is often used to print a string of text, but it can also be used to print the value of a variable or the output of a command.

The echo command has a number of options that allow you to customize its behavior. The one we are interested in is the -e option, which allows you to use escape sequences to include special characters in the output.

That specific option enables us to use the \n special character to print a new line in Bash:

echo -e "This is the first line\nThis is the second line"


This will give us:

This is the first line
This is the second line


#### Using the printf command

In Bash, the printf command is used to display a message or output in the terminal. It is similar to the echo command, but it is generally more powerful and flexible, and it is often preferred over echo for formatting and printing output in Bash scripts.

Like echo, printf can be used to print a string of text, the value of a variable, or the output of a command. However, printf also allows you to specify a format string, which allows you to control the appearance of the output in more detail.

Therefore, there is no need to use any special options/flags (like -e), just write the stirng format between two quotation marks:

printf "This is the first line\nThis is the second line"


As expected, this will yield us with:

This is the first line
This is the second line


Note: In all of these examples, the \n escape sequence is used to represent a new line. The echo and printf commands are used to print the text to the terminal, and the -e option for echo allows it to interpret the escape sequences.

### Conclusion

In conclusion, writing a new line in Bash is an important task that allows you to format the output of your scripts and commands in a way that is easy to read and understand. In this article, we covered two most common ways to write a new line in Bash - using the echo command with the -e option, and the printf command.

Last Updated: December 30th, 2022