How to Echo Newline in Bash? [Complete Guide]

Bash newline is known as a linebreak that marks the end of a line code or command. The newline is represented by the escape sequence \n. It plays a crucial role in structuring commands and scripts effectively. This article aims to provide a comprehensive guide on newlines and methods to echo new lines into text and codes.

A. Echo Newline to Formatted Text

In Bash, sometimes it’s necessary to insert a newline within a lengthy text to make it shorter. It can be done by using the “echo” command, “printf” command, and using the dollar “$” sign.

1. Using “echo” Command

The echo command is the most used approach to insert newline characters \n within long text. You can use the echo command along with the -e option to echo newline. Here the option enables escape  sequence interpretation within the double-quotation "". Check the following code to echo line break or newline within a text:

echo -e "First line\nSecond line"
EXPLANATION
The -e option enables the interpretation of escape sequences in the string.

Echo line breaks within text using the "echo" command with the "-e" option.The output shows the inserted new line within the text line.

I. With “$” Sign

Instead of using the -e option, you can use the dollar sign ($) with the echo command to echo the new. Follow the below code to echo the newline:

echo $'First Line\n Second Line'

EXPLANATION
In the code, the $'...' syntax allows interpretation of certain escape sequences inside the string.

Echo line breaks with the dollar "$" sign.The output displays the text after echoing newline characters within it.

II. Using multiple “echo” statement

You can simply use the echo command multiple times to echo line break in the output as below:

#!/bin/bash

echo "First line"

echo "Second line"

Echo line breaks with the "echo" command multiple times.The output shows the result of echoing line breaks in the text using the echo command multiple times.

III. With Variables

You can use the echo command along with the -e option and the dollar sign ($) to add a line break with a defined variable.

#!/bin/bash

str1='Hello'

str2='World'

echo -e "$str1\n$str2"
EXPLANATION
The script uses the echo command with the -e option to interpret escape sequences, specifically \n which represents a newline character. The dollar sign ($) is used to access the values stored in variables in Bash.

Echo line breaks with variables.A newline is inserted within two variables in the output.

2. Using “printf” Command

You can use the printf command to echo newline characters within the text. Follow the command below to print a line break within the text:

printf "First Line\n Second Line \n"

EXPLANATION
The command printf "First Line\n Second Line \n" prints two lines of text. The \n represents a newline character, causing the text to be displayed on separate lines.

Use the "printf" command to echo line breaks.The output shows the echoed newline characters within the text.

B. Echo Newline Automatically to the Text

In Bash, you can use a loop to automatically echo newline within the text. For example, check the following script to echo newline within text automatically:

#!/bin/bash

string="Hello World"
result=""

for word in $string; do
result="$result$word\n"
done
echo -e "$result"

EXPLANATION
The script initializes a string variable. Then it iterates over each word of the string using a for loop. The script appends each word of the string to the result variable followed by the \n escape sequence using the expression result="$result$word\n". This is how the script echoes the newline within the string.

Echo Bash newline within text automatically.The output shows the string after echoing the newline automatically using the loop.

C. Echo Newline Replacing a Specific Character

In Bash, you can echo newline within the text by replacing a specific character with the line break or newline character (\n) using the “tr”, “sed”, and “awk” commands. This section describes the methods to echo newline by replacing a specific character.

1. Using “tr” Command

The tr command in bash translates or deletes characters. You can use it to replace a specific character with newline characters (\n). Check the following script to replace a specific character with a newline:

#!/bin/bash

string="First Second Third"

echo $string | tr " " "\n"

EXPLANATION
Initially, it defines a variable named string holding the value “First Second Third”. Subsequently, the echo command is employed to display the content of the string variable. The | operator pipes this output as input to the tr command. Within the tr command, it specifies replacing space characters " " with newline characters “\n“.

Echo Bash newline using the "tr" Command.The output displays the text after echoing the newline.

2. Using “sed” Command

The sed command is a powerful tool in Bash for text processing and manipulation. It reads text input line by line, and applies specified operations to each line.

echo "Hello World" | sed 's/ /\n/g'

EXPLANATION
Within the sed command, the expression 's/ /\n/g' is used. Here, s/ /\n/g is a substitution command. It tells sed to find all occurrences of the space character ' ' and replace them with a newline character \n. The g flag ensures that this substitution is applied globally, meaning it will replace all instances of space in the input string, not just the first one.

Echo newline in Bash using the "sed" command.The output shows the string after inserting the line break within it using the sed command.

3. Using “awk” Command

The awk command is effective for searching for patterns and processing files. It’s used in scripts to search through one or multiple files, locate matching patterns, and execute specific tasks. You can use “awk” to replace any specific character with the line break or newline character (\n). Follow the below script:

echo "Hello World" | awk '{gsub(" ", "\n")}1'

EXPLANATION
The gsub(" ", "\n") function within awk replaces each space " " with a newline character \n. The 1 at the end acts as a pattern that always evaluates to true, ensuring awk prints the modified string.

Echo bash newline using the "awk" command.The output shows the string after replacing the space with the newline character (\n).

How to adopt Line-continuation in Long Shell Commands?

Line continuation is the ability to expand a command in multiple lines to break a long command. You can use the backslash \ or single quotation '' to continue a command in multiple lines. To continue a long command in multiple lines by using backslashes \ to continue the command on the next line.

long_command_with_many_arguments \
argument1 \
argument2 \
argument3 \
...

Here’s an example with a longer command using pipes:

cat file.txt \
| grep "pattern" \
| sed 's/foo/bar/g' \
| awk '{print $1}'

Breaking the long commands can greatly improve the readability of complex commands.

[Solved] Echo Command Not Recognizing ‘\n’

When using the ‘echo’ command in Bash, the regular syntax doesn’t interpret escape sequences like \n as newline characters. For example, the following code doesn’t interpret \n as escape sequence.

echo 'Hello\nWorld'

##Output
Hello\nWorld

To have it interpret such escape sequences, you need to use the -e option. So, to display “Hello” on one line and “World” on the next, write the following code:

echo -e 'Hello\nWorld'

##output
Hello
world

Conclusion

To sum up, this guide demonstrates the line break and the processes to echo line breaks within large text. You can use any of the descriptive methods to echo line breaks such as using the echo command, printf command. You can use the “sed”, “awk”, and “tr” commands to replace any specific character with the line break character \n. Hope this article helps you navigate through the process of echoing line breaks within texts.

People Also Ask

What is meant by line break?

A line break, also known as newline, is a sequence of characters (\n) that indicate the end of one line of text and the beginning of the next line within a document, file, or text.

How do I separate lines in Bash?

You can separate lines in Bash by using the echo command with the -e option to interpret the escape sequence (\n) as below:

echo -e "Line 1\nLine 2"

##Output
Line 1
Line 2

Alternatively, you can use the printf command with the newline character \n:

printf "Line 1\nLine 2\n"

##Output
Line 1
Line  2

This is how you can separate lines in Bash.

What is “\n” in Bash?

In Bash, \n is a line break or newline and indicates the start of a new line. When the shell encounters \n in a string, it moves the cursor to the beginning of the next line. This is useful for formatting output or constructing multiline strings in Bash.

What is “\r” in string?

In string, \r is the carriage return character that tells the program or terminal to move the cursor to the beginning of the current line without going to the next line. It’s used in older systems like macOS and some versions of Windows to represent a line break.

How do you break a string line?

To break a string line, you can use the backslash \ at the end of each line to indicate that the string continues on the next line. For example:

string="This is a long string \
that spans multiple lines."

This allows you to break a long string into multiple lines for better readability.

Why is it important to consider line breaks in Bash scripting?

Line breaks in Bash is necessary for several reasons which are:

  1. Readability: Long, complex commands can be challenging to understand when written in a single line. That’s why, breaking them into multiple lines makes it easier to read, and understand.
  2. Maintainability: Breaking long commands into smaller segments allows easier modification, debugging, and troubleshooting.
  3. Error Prevention: Long lines increase the probability of errors such as typos, and syntax errors. By breaking the commands into smaller and manageable lines, developers can reduce the risk of errors.
  4. Collaboration: When multiple developers working on a script, clear and consistent line breaks can improve the collaboration to understand each other’s contribution.

What is line continuation in Bash?

Line continuation in Bash is the ability to extend a command across multiple lines. There are two methods to achieve line continuation in Bash. One is using the backslash \ and another is using the single quotes '' . Using a backslash at the end of a line indicates that the command continues on the next line. Similarly, wrapping the command in a single quote allows it to span multiple lines.

How to print newline in Bash?

In Bash, to print a newline character, you can use the echo command or printf command. You can use the echo command along with option e or the dollar sign $ to interpret the newline character within a string or text as special characters rather than literal backslashes such as echo $'First Line\nSecond Line'. In the case of using printf, you can simply write like this command printf ""First Line\nSecond Line \n to echo newline in Bash.

How to echo the newline character in Bash?

In Bash, you can print a newline character using the echo command with the -e option followed by the newline character \n. For example, to echo a newline within the text, you can use the expression echo -e "First Line\nSecond Line".

How do you line break in the Bash terminal?

In the Bash terminal, you can line break within a long command by simply adding a backslash and pressing ENTER. For example, to count the number of words within a text, you can use the command echo "hello world" | wc -w. Now if want to break the command line in the terminal, you can write echo "hello world" | \ then press ENTER  and write the rest of the command like this wc -w. Both commands will output the same result.

Related Articles


<< Go Back to Bash Multiline StringBash String | Bash Scripting Tutorial

Rate this post
LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now
Auhona Islam

Auhona Islam is a dedicated professional with a background in Electronics and Communication Engineering (ECE) from Khulna University of Engineering & Technology. Graduating in 2023, Auhona is currently excelling in her role as a Linux content developer executive at SOFTEKO to provide a more straightforward route for Linux users. She aims to generate compelling materials for Linux users with her knowledge and skills. She holds her enthusiasm in the realm of Machine Learning (ML), Deep Learning (DL), and Artificial Intelligence (AI). Apart from these, she has a passion for playing instruments and singing. Read Full Bio

Leave a Comment