How to Continue on the Next Line in Bash

Naila Saad Siddiqui Feb 15, 2024
How to Continue on the Next Line in Bash

This brief article is about the explanation of a rule while doing Bash scripting in Linux or UNIX. Before moving toward the main issue, let us briefly introduce Bash programming.

Continue on the Next Line in Bash

Bash is a basic command-line interpreter in the UNIX and Linux operating systems. We can use this interpreter to run specific tasks via the command line, and by placing the commands in a script file, we can run all of the commands at once.

A shell script is just a collection of Bash commands that may be run individually or stored in a script file that the Bash can run later.

In Bash programming, you don’t need any statement terminator; like in C/C++, we need a semicolon at the end of every statement to inform the compiler about the end of the statement. However, the Bash script doesn’t use any statement terminator.

Rather, commands are separated by the lines, i.e., adding a new line marks the end of the statement. This is very convenient and easy to use but creates problems when we have commands that cannot be placed in a single line and cover more space in the next line.

For such lengthy commands, we use a backslash \ symbol to tell the Bash that the command is continued on the next line.

Let us look at the example below:

bash continue on next line - script

In this script, you can see that we have a single echo command, but it is split into multiple lines. When we run the script, it gives the following output:

bash continue on next line - output

Remember that backslash \ should be the last character of the line. There must not be a single character after that, not even whitespace. This backslash \ is known as the escape character in Bash scripting.

Escape Character

A non-quoted backslash is known as the escape character; the purpose of it is to tell the line continuation. It preserves the meaning of any character used after it, except the newline.

If this backslash is combined with newline, this is a line continuation symbol, and the command is continued on the following line.