How to Insert a New Line Character in Linux Shell Script Output ?

In bash, a newline refers to a line's end and marks the beginning of new text. In Linux/Unix operating systems, the newline character is represented as
, which instructs the terminal to move the cursor to the beginning of the next line. Many text editors don't display this character by default. Inserting newline characters serves several important purposes

  • It helps format output to be readable and well-structured.

  • Separating output into sections makes it easier to locate specific information.

  • Newlines act as line breaks that identify where one line ends and another begins in scripts.

  • They allow you to create multi-line output for better presentation.

Sometimes you need to insert newline characters to format and modify your shell script output. This article demonstrates how to insert newline characters in Linux shell script output using various commands and techniques.

Methods to Insert Newline Characters

Using the echo Command with -e Flag

The echo command is commonly used for displaying standard output. By default, echo treats
as literal characters rather than interpreting them as newlines

echo "Tutorial\nPoints"
Tutorial\nPoints

To enable interpretation of escape sequences like
, use the -e flag

echo -e "Tutorial\nPoints"
Tutorial
Points

Note that the -e option may not work consistently across all systems and shell versions, making this approach less portable.

Using Multiple Echo Commands

This approach uses separate echo commands to create newlines. Since echo automatically adds a newline after each output, multiple commands naturally create line breaks

echo "Tutorial"; echo "Points"
Tutorial
Points

The semicolon (;) separates each command, allowing them to execute sequentially on the same line.

Using $ Expansion with Echo

The dollar symbol ($) enables parameter expansion in shell scripts. You can use it with echo to insert newlines, but this method requires careful syntax

echo Tutorial$'<br>'Points
Tutorial
Points

Important considerations for this method

  • Always use the $ sign before the newline character '
    '

  • Enclose
    in single quotes, not double quotes

  • This creates ANSI-C quoting that interprets escape sequences

Using the printf Command

The printf command provides more precise control over output formatting and consistently interprets escape sequences across all systems

printf "Tutorial\nPoints<br>"
Tutorial
Points

Unlike echo, printf doesn't automatically add a trailing newline, so you must explicitly include
where needed. This command works reliably across all Unix-like systems without requiring special flags.

Comparison of Methods

Method Portability Performance Ease of Use
echo -e Limited Fast Simple
Multiple echo High Fast Very Simple
$ expansion Good Fast Moderate
printf Excellent Slower Flexible

Practical Examples

Here's a shell script demonstrating multiple newline insertions

#!/bin/bash
printf "Line 1\nLine 2<br>\nLine 4 (with blank line above)<br>"
echo -e "Using echo:\nFirst line\nSecond line"
Line 1
Line 2

Line 4 (with blank line above)
Using echo:
First line
Second line

Conclusion

There are multiple ways to insert newline characters in Linux shell script output. The printf command offers the most reliable and portable solution, while echo with various approaches provides faster alternatives. Choose the method that best fits your script's portability requirements and complexity needs.

Updated on: 2026-03-17T09:01:38+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements