Categories
bash echo newline

Echo newline in Bash prints literal \n

3170

How do I print a newline? This merely prints \n:

$ echo -e "Hello,\nWorld!"
Hello,\nWorld!

11

  • 337

    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).

    Dec 12, 2011 at 1:58

  • 8

    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.

    Jan 10, 2015 at 3:28


  • 8

    Also notable: in Unix & Linux Stack Exchange, the accepted answer to How to add new lines when using echo

    Apr 9, 2016 at 7:02

  • 2

    echo -ne “hello\nworld” (you needed the n flag to interpret escapes) – but as others say, different echo commands may have different results!

    – Konchog

    Mar 28, 2018 at 7:00

  • 3

    @Konchog echo -n man page entry on archlinux ` -n do not output the trailing newline` It has nothing to do with interpreting escapes

    – user12207064

    May 27, 2020 at 18:19

3769

Use printf instead:

printf "hello\nworld\n"

printf behaves more consistently across different environments than echo.

19

  • 59

    or even printf %"s\n" hello world — printf will reuse the format if too many arguments are given

    Dec 12, 2011 at 0:57


  • 47

    The OP asked about echo, not printf; and @choroba’s answer below, which uses the -e option, fills the bill perfectly.

    – JESii

    May 27, 2015 at 13:46


  • 81

    @JESii: It fits if your echo happens to support the -e option.

    – sth

    May 27, 2015 at 13:57

  • 25

    With some versions of echo, -e is just printed in the output itself so I think this answer is perfectly valid since echo isn’t consistent here (unless we’re talking about a specific version).

    Jun 10, 2015 at 19:47

  • 18

    This is well and good if printf is available, but unlike echo sometimes printf isn’t on the distro.

    Aug 18, 2015 at 13:53

2169

Make sure you are in Bash.

$ echo $0
bash

All these four ways work for me:

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

15

  • 463

    -e flag did it for me, which “enables interpretation of backslash escapes”

    – tandy

    Aug 7, 2013 at 20:52


  • 29

    I think -e param doesn’t exist on all *nix OS

    – kenorb

    Sep 4, 2013 at 15:28

  • 16

    @kenorb: It exists in bash. It is a builtin.

    – choroba

    Sep 4, 2013 at 20:09


  • 3

    Why does the third one work? Without the $ it returns “Hello n world”

    Nov 11, 2013 at 21:33

  • 37

    As mentioned by various other -e does NOT work for all distributions and versions. In some cases it is ignored and in others it will actually be printed out. I don’t believe this fixed it for the OP so should not be accepted answer

    – csga5000

    Apr 15, 2016 at 4:23


708

+50

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.

7

  • 16

    @EvgeniSergeev Not sure what you mean, but it didn’t work for me either first. And that’s because I was using double quotes and turns out this works only with single quotes! Tried in Terminal on Mac.

    – trss

    Oct 1, 2016 at 5:49

  • 13

    Problems with variables in the string not being expanded.

    – willemdh

    Nov 3, 2016 at 16:10

  • You can still concatenate double-quote strings. ` foo=”bar”; echo $”$foo’efoot’`

    Sep 3, 2019 at 0:24


  • This is what I wanted. Works fine on Xenial.

    Jul 23, 2020 at 13:32

  • It woks on GNU bash, version 4.4.23(1)-release (x86_64-pc-msys) W10 like a charm.

    Aug 13, 2021 at 0:15