FUNDAMENTALS A Complete Guide for Beginners
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.
6 Practical Cases of Echo New Line in Bash
There are many practical cases of echoing new line which you can use in your Bash Script. Here’s 6 most common practical cases of echoing new line:
1. Echo Single New Line in Bash
Follow the steps below to make a script that echoes 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"
EXPLANATIONHere, 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.
2. Echo Multiple New Lines Using Bash Script
Here’s how to echo multiple new lines in a Bash script:
#! /bin/bash
echo -e "Hello\n\n\nWorld"
There is “Hello” followed by three “\n”. So the interpreter will print three newlines below “Hello”. Here you can see multiple new lines between “Hello” and “World”. Use the escape character \t to insert a new line tab. Here’s the script: Here 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: The In In this example, I will create two separator lines and print a text in between: 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.” Follow the below script to use new lines with multiple statements: Here 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. To echo a new line in Bash, you need to use To echo without a new line in bash, you need to use the option 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. If you want to echo multiple lines in a file, you need to use echo with the option Related Articles << Go Back to Bash Output | Bash I/O | Bash Scripting Tutorial
3. Echo a New Line With Tab in Bash
#! /bin/bash
echo -e "Hello\n\tWorld"
\n
and \t
is used to print new line character and tab character respectively.
4. Echo a New Line With Command Substitution in Bash
#! /bin/bash
date=$(date +%Y-%m-%d)
echo -e "Today is $(date).\nCurrent date is $date."
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.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.5. Echo a New Line With Separator Using Bash Script
#! /bin/bash
echo -e "-----\nHello World\n-----"
6. Echo a New Line With Multiple Statements Using Bash Script
#! /bin/bash
echo -e "Hello World\n" && echo -e "Welcome to LinuxSimply!!\n"
&&
(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.Conclusion
People Also Ask
How do I echo a new line in Bash?
\n
with the option -e
of the echo command. For example: echo -e “Hello\nWorld”
will echo a new line between “Hello” and “World”.How do I echo without a new line in Bash?
-n
of the echo command. As you will see, the output does not have a newline character.What is the \n in shell script?
How do I echo multiple lines in a file?
-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