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

LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now

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:

  1. At first, launch a Terminal in Ubuntu.
  2. Copy the following command to open a file in Nano:
    nano single_line.sh
  3. Write the script mentioned below:
    #! /bin/bash
    
    echo -e "Hello\nWorld"
    EXPLANATION

    Here, 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.

  4. Press CTRL+O and ENTER to save the file; CTRL+X exit.
  5. Use the following command to make the file executable:
    chmod u+x single_line.sh

    Making a file Executable

  6. 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.

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"

EXPLANATION

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

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

3. Echo a New Line With Tab in Bash

Use the escape character \t to insert a new line tab. Here’s the script:

#! /bin/bash

echo -e "Hello\n\tWorld"

EXPLANATION

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

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

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:

#! /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.

Executing "command_sub.sh" script

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:

#! /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.”

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

6. Echo a New Line With Multiple Statements Using Bash Script

Follow the below script to use new lines with multiple statements:

#! /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.

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” will echo a new line between “Hello” and “World”.

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 \n 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