Echo newline in Bash prints literal in Bash \n

In bash scripting, the echo command displays the output on the screen, for example, a message of “Welcome to ItsLinuxFoss” can be displayed on the screen using the echo command in bash scripting. 

Different options can be utilized in bash scripting to print the content on the screen. Similarly, to increase readability, some text should be printed, starting from the next line on the screen. For this purpose, the notation of “\n” can be used with the echo command in the bash scripting. 

This post explains the usage of the “\n” with the echo command in bash scripting to print the message on the next line with some examples. 

This post will walk through the following topics for the understanding of the echo command:

  1. What is the echo Command in Bash Scripting in Linux
  2. What is the Usage of the “\n” with the echo Command in Bash Scripting?
  3. What is the Common Possible Issue in Using the \n with the echo Command in Bash Scripting?

Let’s start with the introduction of the echo command in bash scripting in Linux. 

What is the echo Command in Bash Scripting in Linux?

The echo command in bash scripting displays the output on the screen. Mostly it is used to print the messages or values stored in the variables on the screen. The general syntax of using the echo command in bash scripting is:

$ echo [options] "[string]"    #if the string is directly displayed

$ echo [options] $variable     #if the string is stored in the variable

If the messages are supposed to display on the screen using the echo command, then type the message in the inverted commas with the echo command. For example, to display the message “Welcome to ItsLinuxFoss” as shown below:

$ echo "Welcome to ItsLinuxFoss"

If the value stored in the variable is supposed to display on the screen, then type the variable name with the echo command as illustrated below:

CompanyName="ItsLinuxFoss"

echo $CompanyName

What is the Usage of the “\n” with the echo Command in Bash Scripting?

In the bash scripting, it is used with the echo command for displaying the output in the next new line. For example, to display the message “Welcome to ItsLinuxFoss. This is Bash scripting tutorial.” in two separate lines, use the command:

$ echo "Welcome to ItsLinuxFoss."$'\n'"This is a Bash scripting tutorial."

It can be seen in the above figure, that the Dollar $ sign has been used with the ‘\n’ to enter a new line between the message. 

Another way to enter a newline in bash scripting with the echo command is to use the “-e” option of the echo command. This option enables the backlash recognition by the echo command. For example, to print the above message with the “e” option, run the command:

$ echo -e "Welcome to ItsLinuxFoss.\nThis is a Bash scripting tutorial."

Also one can run the above command with the single inverted commas instead of the double to have the same output:

$ echo -e 'Welcome to ItsLinuxFoss.\nThis is a Bash scripting tutorial.'

The message has been split into two different lines successfully as seen in the output. 

What is the Common Possible Issue in Using the \n with the echo Command in Bash Scripting?

Mostly the users directly insert the “\n” option in the message to break the message into two lines. Unfortunately, the message will be displayed in a single line below:

$ echo "Welcome to ItsLinuxFoss.\nThis is a Bash scripting tutorial."

This happened because the backlash has not been recognized by the echo command. To enable the recognition of the backlash by the echo command, use its “e” option:

$ echo -e "Welcome to ItsLinuxFoss.\nThis is a Bash scripting tutorial."

What are the Other Methods to Print a Newline in the Bash Scripting?

There are many other methods instead of “\n” that can be used to split the message in two or more than two lines. The methods are explained below with examples for better understanding.

Use the printf

Another method to display the output on the screen in bash scripting is by using the printf command:

$ printf  "Welcome to ItsLinuxFoss.\nThis is a Bash scripting tutorial.\n"

Use the Semicolon

The semicolon “;” can also be used to split the message into two different lines in bash scripting:

$ echo Welcome ; echo ItsLinuxFoss

These are two methods that can be used to add the new line in the bash scripting other than the \n with the echo command. 

That’s all about the usage of the \n with the echo command in bash scripting. 

Conclusion

The \n is used with the echo command to add a new line in the bash scripting. Either use the “e” option of the echo command with the \n notation or use the “$” sign with it. This blog is useful to understand the addition of the newline in the bash scripting with the usage of \n with the echo command. Other than this, two different methods have also been explained in this post.