2846
votes

In Bash, tried this:

echo -e "Hello,\nWorld!"

But it doesn't print a newline, only \n. How can I make it print the newline?

I'm using Ubuntu 11.04 (Natty Narwhal).

23
For those saying "it works for me", the behavior of echo varies quite a bit between versions. Some will even print the "-e" as part of their output. If you want predictable behavior for anything nontrivial, use printf instead (as in @sth's answer).Gordon Davisson
I could not get any of the suggestions in this answer working, because, as it turns out, I was attempting to use it in a function that returns a value, and all the echo (and printf) messages in the function were being appended to the return value after being individually stripped of newlines. Here is a question regarding this, with an extremely thorough answer: stackoverflow.com/questions/27872069/… This was like a three hour mystery tour.aliteralmind
Also notable: in Unix & Linux Stack Exchange, the accepted answer to How to add new lines when using echoGraham Perrin
echo -ne "hello\nworld" (you needed the n flag to interpret escapes) - but as others say, different echo commands may have different results!Konchog
@Konchog echo -n man page entry on archlinux ` -n do not output the trailing newline` It has nothing to do with interpreting escapesuser12207064

23 Answers

3438
votes

You could use printf instead:

printf "hello\nworld\n"

printf has more consistent behavior than echo. The behavior of echo varies greatly between different versions.

1975
votes

Make sure you are in Bash. All these four ways work for me:

echo -e "Hello\nworld"
echo -e 'Hello\nworld'
echo Hello$'\n'world
echo Hello ; echo world
645
votes
echo $'hello\nworld'

prints

hello
world

$'' strings use ANSI C Quoting:

Words of the form $'string' are treated specially. The word expands to string, with backslash-escaped characters replaced as specified by the ANSI C standard.

163
votes

You could always do echo "".

For example,

echo "Hello,"
echo ""
echo "World!"
88
votes

Try

echo -e "hello\nworld"
hello
world

It worked for me in the nano editor.

From the man page:

-e enable interpretation of backslash escapes

47
votes

In the off chance that someone finds themselves beating their head against the wall trying to figure out why a coworker's script won't print newlines, look out for this:

#!/bin/bash
function GET_RECORDS()
{
   echo -e "starting\n the process";
}

echo $(GET_RECORDS);

As in the above, the actual running of the method may itself be wrapped in an echo which supersedes any echos that may be in the method itself. Obviously I watered this down for brevity. It was not so easy to spot!

You can then inform your comrades that a better way to execute functions would be like so:

#!/bin/bash
function GET_RECORDS()
{
   echo -e "starting\n the process";
}

GET_RECORDS;
32
votes

Simply type

echo

to get a new line

29
votes

This works for me in Raspbian,

echo -e "hello\\nworld"
27
votes

POSIX 7 on echo

http://pubs.opengroup.org/onlinepubs/9699919799/utilities/echo.html

-e is not defined and backslashes are implementation defined:

If the first operand is -n, or if any of the operands contain a <backslash> character, the results are implementation-defined.

unless you have an optional XSI extension.

So I recommend that you should use printf instead, which is well specified:

format operand shall be used as the format string described in XBD File Format Notation [...]

the File Format Notation:

\n <newline> Move the printing position to the start of the next line.

Also keep in mind that Ubuntu 15.10 and most distros implement echo both as:

  • a Bash built-in: help echo
  • a standalone executable: which echo

which can lead to some confusion.

16
votes
str='hello\nworld'
$ echo | sed "i$str"
hello
world
10
votes

You can also do:

echo "hello
world"

This works both inside a script and from the command line.

On the command line, press Shift+Enter to do the line break inside the string.

This works for me on my macOS and my Ubuntu 18.04 (Bionic Beaver) system.

8
votes

It works for me in CentOS:

echo -e ""hello\nworld""
6
votes

I just use echo without any arguments:

echo "Hello"
echo
echo "World"
5
votes

My script:

echo "WARNINGS: $warningsFound WARNINGS FOUND:\n$warningStrings

Output:

WARNING : 2 WARNINGS FOUND:\nWarning, found the following local orphaned signature file:

On my Bash script I was getting mad as you until I've just tried:

echo "WARNING : $warningsFound WARNINGS FOUND:
$warningStrings"

Just hit Enter where you want to insert that jump. The output now is:

WARNING : 2 WARNINGS FOUND:
Warning, found the following local orphaned signature file:
4
votes

This could better be done as

x="\n"
echo -ne $x

-e option will interpret backslahes for the escape sequence
-n option will remove the trailing newline in the output

PS: the command echo has an effect of always including a trailing newline in the output so -n is required to turn that thing off (and make it less confusing)

4
votes

If you're writing scripts and will be echoing newlines as part of other messages several times, a nice cross-platform solution is to put a literal newline in a variable like so:

newline='
'

echo "first line$newlinesecond line"
echo "Error: example error message n${newline}${usage}" >&2 #requires usage to be defined
4
votes

One more entry here for those that didn't make it work with any of these solutions, and need to get a return value from their function:

function foo()
{
    local v="Dimi";
    local s="";
    .....
    s+="Some message here $v $1\n"
    .....
    echo $s
}

r=$(foo "my message");
echo -e $r;

Only this trick worked on a Linux system I was working on with this Bash version:

GNU bash, version 2.2.25(1)-release (x86_64-redhat-linux-gnu)
4
votes

There is a new parameter expansion added in Bash 4.4 that interprets escape sequences:

${parameter@operator} - E operator

The expansion is a string that is the value of parameter with backslash escape sequences expanded as with the $'…' quoting mechanism.

$ foo='hello\nworld'
$ echo "${foo@E}"
hello
world
3
votes

You could also use echo with braces,

$ (echo hello; echo world)
hello
world
1
votes

This may not apply in your case, but it's something that has confused me in the past:

Wrong

Writing "hello\nworld" in Bash gives you a string with a new-line character in it, and echo -e prints that exact string.

Right

Writing $'hello\nworld' or

"hello
world"

gives you a string with a new-line character in it, and plain echo prints that exact string. Which is good, since as you've seen, echo -e isn't always supported.

0
votes

Sometimes you can pass multiple strings separated by a space and it will be interpreted as \n.

For example when using a shell script for multi-line notifcations:

#!/bin/bash
notify-send 'notification success' 'another line' 'time now '`date +"%s"`
0
votes

This got me there....

outstuff=RESOURCE_GROUP=[$RESOURCE_GROUP]\\nAKS_CLUSTER_NAME=[$AKS_CLUSTER_NAME]\\nREGION_NAME=[$REGION_NAME]\\nVERSION=[$VERSION]\\nSUBNET-ID=[$SUBNET_ID]
printf $outstuff

Yields:

RESOURCE_GROUP=[akswork-rg]
AKS_CLUSTER_NAME=[aksworkshop-804]
REGION_NAME=[eastus]
VERSION=[1.16.7]
SUBNET-ID=[/subscriptions/{subidhere}/resourceGroups/makeakswork-rg/providers/Microsoft.Network/virtualNetworks/aks-vnet/subnets/aks-subnet]
-4
votes

Additional solution:

In cases, you have to echo a multiline of the long contents (such as code/ configurations)

For example:

  • A Bash script to generate codes/ configurations

echo -e, printf might have some limitation

You can use some special char as a placeholder as a line break (such as ~) and replace it after the file was created using tr:

echo ${content} | tr '~' '\n' > $targetFile

It needs to invoke another program (tr) which should be fine, IMO.