Echo Multiple Lines in Bash

Bash echo multiple lines

1. Overview

In this article, we will see how to echo multiple lines, primarily using the echo command in various forms. Additionally, we will look at alternative methods like printf and here documents.

2. Introduction to Problem Statement

Our goal is to print multiple lines with various options.

The output of every method will be as follows:

We won’t repeat the output in the below methods as it will remain the same.

3. Using echo Command

The echo is a built-in command primarily used to display lines of text or string on the standard output or a file.

We can use the echo command differently to display multiple lines in one prompt on the Bash console. Let’s learn them one by one.

3.1 Using echo with NewLine Characters

The most straightforward method to print multiple lines is by putting newline characters(\n) within the string. For example:

The -e option enables the backslash escape sequence’s interpretation in string. In the above example, we used the n escape sequence, representing a newline character, resulting in the multiline string output on the Bash console.

3.2 Using Multiple echo Commands

Alternatively, we can use multiple echo commands, each printing a single line:

This method is simple but can be lengthy for script with many lines to print.

3.3 Using echo with Quoting

Using single or double quotes, we can print multiple lines as:

We can use double quotes to have a single quote in one or multiple lines; see the following example.

Similarly, we can use single quotes to print multiple lines. In this case, we can have multiple double quotes in the string.

3.4 Using echo with a Shell Variable

We can store multi-line content in a variable and echo it:

This example is the same as using echo with double quotes and single quotes, but it is preferred when we want to store the multiline string in a shell variable before displaying it on the console.

We can also do it using single quotes; see the following example.

4. Using printf with Newline Characters

The printf is a more powerful command than echo, as it can provide formatted output.

5. Using a Here Document

We can use a here document, also known as here document operator, to echo multiple lines on the Bash console.

In this example, the cat command reads the input from the Here Document until it encounters EOF, denoting the end of the text block.

This method is useful and efficient for large blocks of text and maintains the formatting and indentation.

Let’s understand more about Here Document.

Here is the syntax of here document:

command: The command we want to pass the text block to.
<<: This symbol initiates the Here Document.
**[delimiter]: A unique identifier (often uppercase, like EOF) that marks the beginning and end of the text block.

The << operator (a.k.a here document operator) is used to specify the start of the here document. It permitted us to provide multiple lines of input to a command or program directly from the command line without needing an external file.

While using here-document, we do not have to enclose your text within the single or double quotes.

6. Using the read Command with the Here Document

We can use the read command to echo (display) multiple lines on the Bash console.

This example is similar to the code snippet in the previous section, with a few additional things.

The above code read the multiline string and assigned it to the multiline_string variable using the here document. Then, it used the echo command to print the value of the multiline_string variable on the console.

Here, the -r option ensured that backslashes within the given input would not be taken as escape characters but read as literal characters, while the -d option set the delimiter to an empty string (''), meaning the read command would read the input until it encounters the EOF (End of File) marker.

This approach is practical for scripts that require user input or configuration.

7. Conclusion

In this article, we discussed ways to echo multiple lines over the bash console.

echo with -e option provides a fast and straightforward to echo multiple lines, while printf offers more control over the format.

Here Document is an ideal choice for large, static text, and combining read with here document provides interactive user input options.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *