How to Echo New Line in Bash [6 Practical Cases]

By default, the echo command prints text separated by spaces and adds a new line at the end.  However, if you want to insert a new line within your text, you need to use the escape character “\n” that represents a new line. Moreover, remember that you need to use the option “-e” of the echo command to use escape characters as “\n”. In this article, I will show you how to echo a new line in Bash and how to use it in different cases.

Key Takeaways

  • Learning to Echo New Line.
  • Different cases of Echo New Line.

Free Downloads

6 Practical Cases of Echo New Line in Bash

Here I will provide some practical cases of echoing new line which you can use in your Bash Script.

Case 1: Echo Single New Line in Bash

In the beginning, I will show you how to insert a single new line.

Steps to Follow >

❶ At first, launch a Terminal in Ubuntu.

❷ Copy the following command to open a file in Nano:

nano single_line.sh

Opening a file for "Bash Echo New Line"

EXPLANATION
  • nano: Opens a file in the Nano text editor.
  • sh: Name of the script.

❸ Write the script mentioned below:

#! /bin/bash

echo -e "Hello\nWorld"

EXPLANATION

Here #!/bin/bash is called Shebang which specifies the interpreter. Then the echo command is used to print text on the terminal. In the text, there is “Hello”, followed by “\n”. The “-e” option of echo interprets “\n” as a newline character.

❹ Press CTRL+O and ENTER to save the file; CTRL+X exit.

❺ Use the following command to make the file executable:

chmod u+x single_line.sh

Making a file Executable

EXPLANATION
  • chmod: Changes permissions.
  • u+x: Giving the owner executing permission.
  • sh: Name of the script.

❻ Run the script by the following command:

./single_line.sh

Running "single_line.sh" scriptIn the output, you can see the word “World” is printed in the second line.

Case 2: Echo Multiple New Lines Using Bash Script

In this example, you will learn how to insert multiple new lines inside a text.

You can follow the Steps of Case 1 to know about creating and saving shell scripts.

Script (multiple_lines.sh) >

#! /bin/bash

echo -e "Hello\n\n\nWorld"

EXPLANATION

There is “Hello” followed by three “\n”. So the interpreter will print three newlines below “Hello”.

Now use the following command to execute the script

./multiple_lines.sh

Executing "multiple_lines.sh" scriptHere you can see multiple new lines between “Hello” and “World”.

Case 3: Echo a New Line With Tab in Bash

Now I will use the escape character “\t” to insert a new line tab.

You can follow the Steps of Case 1 to know about creating and saving shell scripts.

Script (tabs.sh) >

#! /bin/bash

echo -e "Hello\n\tWorld"

EXPLANATION

Here “\n” and “\t” is used to print new line character and tab character respectively.

Now execute the script by the following command

./tabs.sh

Running "tabs.sh" script for "Bash Echo New Line"In the output, there is a tab followed by the word “World”.

Case 4: Echo a New Line with Command Substitution in Bash

In this example, I will echo a new line and substitute a command at the same time. Here, I will use the date command to print the date.

You can follow the Steps of Case 1 to know about creating and saving shell scripts.

Script (command_sub.sh) >

#! /bin/bash

date=$(date +%Y-%m-%d)
echo -e "Today is $(date).\nCurrent date is $date."

EXPLANATION

The date=$(date +%Y-%m-%d) line assigns the output of the “datecommand to the “datevariable. The date command is executed with the format option %Y-%m-%d that returns the current date in the format ‘Year-Month-Day’. Moreover, “$()” is used to capture the output of the date command.

In echo -e “Today is $(date).\nCurrent date is $date.” line, the echo command is used to print text to the terminal. First, it prints “Today is”, followed by the current date obtained by the “date” command inside the command substitution “$()”. Then “\n” adds a new line. Finally, it displays the string “Current date is”, followed by the value of the “date” variable.

Run the script by the following command

./command_sub.sh

Executing "command_sub.sh" script

Case 5: Echo a New Line with Separator Using Bash Script

In this example, I will create two separator lines and print a text in between.

You can follow the Steps of Case 1 to know about creating and saving shell scripts.

Script (separator.sh) >

#! /bin/bash

echo -e "-----\nHello World\n-----"

EXPLANATION

Here the separator line itself consists of a series of dashes (“—–”) which acts as a visual separator. Here “\n” is used two times. At first, the first separator is printed followed by a new line. Then “Hello World” is displayed. Finally, a new line is created, followed by the second separator.”

Execute the script by the command below

./separator.sh

Running "separator.sh" scriptHere in the output, as you can see, the string “Hello World” is displayed between two separators.

Case 6: Echo a New Line with Multiple Statements Using Bash Script

Finally, I will show you how to use new lines with multiple statements.

You can follow the Steps of Case 1 to know about creating and saving shell scripts.

Script (multi_state.sh) >

#! /bin/bash

echo -e "Hello World\n" && echo -e "Welcome to LinuxSimply!!\n"

EXPLANATION

Here ‘&&’ (AND) operator is a control operator in Bash. It allows you to execute multiple commands sequentially, where the next command is executed only if the previous one succeeds. Here both statements are executed with a new line at the end.

Run the script using the command below

./multi_state.sh

Executing "multi_state.sh" script

Conclusion

In this article, I have tried my best to show all the possible cases of echo new line in Bash. Try to run all the scripts on your own. Also, review the explanations to grasp the full concept.

People Also Ask

How do I echo a new line in Bash?
To echo a new line in Bash, you need to use “\n” with the option “-e” of the echo command. For example: echo -e “Hello\nWorld”
How do I echo without a new line in bash?
To echo without a new line in bash, you need to use the option “-n” of the echo command. As you will see, the output does not have a newline character.
What is the in shell script?
The \n in shell script is an escape character that represents a newline. Escape characters are used to insert special characters that cannot be directly typed or displayed.
How do I echo multiple lines in a file?
If you want to echo multiple lines in a file, you need to use echo with the option “-e” and escape character “\n”. Also, you need to redirect the output to a file. For instance: echo -e “line 1\nline 2\nline 3” > file.txt

Related Articles


<< Go Back to Bash Output | Bash I/O | Bash Scripting Tutorial

Rate this post
Walid Al Asad

Hello Everyone! I am Walid Al Asad. Currently, I am working at a tech company named Softeko as a Linux Content Developer Executive. I live in Dhaka, Bangladesh. I have completed my BSc. in Mechanical Engineering from Bangladesh University of Engineering and Technology (BUET). You can find me on LinkedIn, and ResearchGate. Read Full Bio

Leave a Comment