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.
- Learning to Echo New Line.
- Different cases of Echo New Line.
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.
❶ At first, launch a Terminal in Ubuntu.
❷ Copy the following command to open a file in Nano:
❸ Write the script mentioned below:
#! /bin/bash echo -e "Hello\nWorld"
❹ 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
❻ Run the script by the following command:
In 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.
Script (multiple_lines.sh) >
#! /bin/bash echo -e "Hello\n\n\nWorld"
Now use the following command to execute the script
Here 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.
Script (tabs.sh) >
#! /bin/bash echo -e "Hello\n\tWorld"
Now execute the script by the following command
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.
Script (command_sub.sh) >
#! /bin/bash date=$(date +%Y-%m-%d) echo -e "Today is $(date).\nCurrent date is $date."
Run the script by the following command
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.
Script (separator.sh) >
#! /bin/bash echo -e "-----\nHello World\n-----"
Execute the script by the command below
Here 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.
Script (multi_state.sh) >
#! /bin/bash echo -e "Hello World\n" && echo -e "Welcome to LinuxSimply!!\n"
Run the script using the command below
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 to Print Output in Bash [With 6 Practical Examples]
- What is Echo Command in Bash [With 3 Practical Examples]
- Cat Command in Bash [With 4 Practical Examples]
- How to Save Bash Output to File? [3 Practical Cases]
- How to Save Bash Output to Variable? [With Practical Cases]
- How to Set Command Output to Variable in Bash [2 Methods]
- How to Suppress Output in Bash [3 Cases With Examples]
- How to Change Color of Output Using Bash Script? [Easy Guide]