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.
❶ At first, launch a Terminal in Ubuntu.
❷ Copy the following command to open a file in Nano:
nano single_line.sh
❸ Write the script mentioned below:
#! /bin/bash
echo -e "Hello\nWorld"
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
❻ Run the script by the following command:
./single_line.sh
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.
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"
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
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.
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"
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
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."
The date=$(date +%Y-%m-%d) line assigns the output of the “date” command to the “date” variable. 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
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-----"
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
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.
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"
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
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
Related Articles
- 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]
<< Go Back to Bash Output | Bash I/O | Bash Scripting Tutorial