How to echo newline
Content on WhatAnswers is provided "as is" for informational purposes. While we strive for accuracy, we make no guarantees. Content is AI-assisted and should not be used as professional advice.
Last updated: April 4, 2026
Key Facts
- The escape sequence for a newline is `\n`.
- The `echo -e` command interprets escape sequences.
- `printf` is another common command for formatted output, including newlines.
- Some shells might automatically interpret newlines without `-e`.
- The behavior can vary slightly between different operating systems and shells.
Overview
Echoing a newline character is a fundamental operation in command-line interfaces (CLIs) and programming. It allows you to control the formatting of text output, making it more readable by separating lines. Whether you're scripting in Bash, Zsh, or using the Windows Command Prompt, understanding how to insert a newline is essential for presenting information clearly.
Understanding Newlines
A newline character, often represented as `` (in many programming languages and shell contexts) or sometimes as `CRLF` (Carriage Return and Line Feed, common on Windows), signals the end of a line and the beginning of a new one. When a program or command outputs text containing a newline character, the cursor moves to the beginning of the next line on the display.
Methods for Echoing Newlines
Using `echo -e` (Common in Unix-like Shells)
In shells like Bash and Zsh found on Linux and macOS, the `echo` command is frequently used. By default, `echo` often just prints its arguments followed by a newline. However, to explicitly insert a newline character within a string or to control output formatting more precisely, you can use the `-e` option. This option enables the interpretation of backslash escape sequences.
The most common escape sequence for a newline is `\n`. So, to print two lines of text, you would do:
echo -e "First line\nSecond line"
This command will output:
First lineSecond line
It's important to note that the behavior of `echo` can sometimes be inconsistent across different shells and operating system versions. Some versions of `echo` might not support the `-e` option or might interpret escape sequences differently. For maximum portability, especially in shell scripting, `printf` is often recommended.
Using `printf` (More Portable)
The `printf` command is generally considered more portable and reliable for formatted output than `echo`, especially when dealing with escape sequences. It follows the C standard library's `printf` function behavior more closely.
To achieve the same result as the `echo -e` example, you would use `printf` like this:
printf "First line\nSecond line\n"
Or, to print a single string with an embedded newline:
printf "Hello\nWorld"
This will output:
HelloWorld
The `printf` command does not automatically add a trailing newline unless you explicitly include `\n` at the end of your format string. This gives you finer control over the output.
Windows Command Prompt (`cmd.exe`)
In the traditional Windows Command Prompt (`cmd.exe`), echoing newlines requires a slightly different approach. The `echo` command works differently, and the concept of escape sequences like `\n` is not directly supported in the same way.
To print a newline, you can often use `echo.` (echo followed by a period). This command prints a blank line. To print text followed by a newline, you can use `echo` with redirection:
echo First line & echo Second line
This will print "First line", then move to the next line, and then print "Second line".
For more complex formatting or to embed newlines within a single command, you might need to use batch scripting techniques or explore alternatives like PowerShell.
Windows PowerShell
PowerShell offers a more modern and flexible command-line experience on Windows. It integrates well with .NET and provides powerful cmdlets for output management.
In PowerShell, you can use the `Write-Host` cmdlet or the `echo` (alias for `Write-Output`) cmdlet. To include a newline, you can use the `n` character, often enclosed in double quotes to allow variable expansion and escape sequence interpretation. PowerShell also understands the standard `` escape sequence within strings delimited by double quotes.
Write-Host "First line`nSecond line"
Note the use of the backtick (`) character before `n` in some contexts, which acts as an escape character in PowerShell for certain special sequences, although `` is generally understood within double-quoted strings.
Write-Host "Hello`nWorld"
Alternatively, and often more simply within double quotes:
Write-Host "Hello\nWorld"
Or using `echo` (which is an alias for `Write-Output`):
echo "First line\nSecond line"
PowerShell's `Write-Output` cmdlet is generally preferred for output that might be piped to other commands, while `Write-Host` is for direct console display.
Why is Echoing Newlines Important?
Readability: Separating text into distinct lines significantly improves the readability of output, especially for logs, reports, or user messages. Scripts that produce lengthy output can become incomprehensible without proper line breaks.
Formatting: Controlling newlines allows for structured presentation of data, such as creating simple tables or formatted lists directly in the terminal.
Scripting: In shell scripts, precisely controlling output formatting is crucial for automation. Whether the output is for human consumption or for parsing by another script, predictable newline placement is key.
Conclusion
Mastering how to echo newlines is a small but significant skill for anyone working with command-line tools. While `echo -e` is common in Unix-like systems, `printf` offers greater portability. Windows users will find different syntax in `cmd.exe` and PowerShell. Understanding these variations ensures your commands and scripts produce clear, well-formatted output across different environments.
More How To in Daily Life
Also in Daily Life
More "How To" Questions
Trending on WhatAnswers
Browse by Topic
Browse by Question Type
Sources
- Shell Expansions - Bash ManualGPL-3.0-or-later
- Write-Host Cmdlet - PowerShell Documentationfair-use
- ECHO - SS64 Command Line Referencefair-use
Missing an answer?
Suggest a question and we'll generate an answer for it.