Table of Contents
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:
1 2 3 4 5 |
It is line 1. It is line 2. It is line 3. |
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:
1 2 3 |
echo -e "It is line 1.\nIt is line 2.\nIt is line 3." |
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:
1 2 3 4 5 |
echo "It is line 1." echo "It is line 2." echo "It is line 3." |
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:
1 2 3 4 5 |
echo "It is line 1. It is line 2. It is line 3." |
We can use double quotes to have a single quote in one or multiple lines; see the following example.
1 2 3 4 5 |
echo "It is line 1. Isn't it? It is line 2. It is line 3." |
Similarly, we can use single quotes to print multiple lines. In this case, we can have multiple double quotes in the string.
1 2 3 4 5 |
echo 'It is "line 1". It is line 2. It is line 3.' |
3.4 Using echo with a Shell Variable
We can store multi-line content in a variable and echo it:
1 2 3 4 5 6 |
multiline_string="It is line 1. It is line 2. It is line 3." echo "$multiline_string" |
1 2 3 4 5 |
It is line 1. It is line 2. It is line 3. |
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.
1 2 3 4 5 6 |
multiline_string='It is line 1. It is line 2. It is line 3.' echo "$multiline_string" |
4. Using printf with Newline Characters
The printf
is a more powerful command than echo, as it can provide formatted output.
1 2 3 |
printf "It is line 1.\nIt is line 2.\nIt is line 3." |
Further reading:
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.
1 2 3 4 5 6 7 |
cat << EOF It is line 1. It is line 2. It is line 3. EOF |
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:
1 2 3 4 5 |
command << [delimiter] text block [delimiter] |
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.
1 2 3 4 5 6 7 8 |
read -r -d '' multiline_string << EOF It is line 1. It is line 2. It is line 3. EOF echo "$multiline_string" |
1 2 3 4 5 |
It is line 1. It is line 2. It is line 3. |
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.