Why does the following command not insert new lines in the generated file and what's the solution?

$ echo "Line 1\r\nLine2" >> readme.txt

$ cat readme.txt 
Line 1\r\nLine2
  • 10
    Is echo mandatory ? you can use printf.
    – Archemar
    Commented Jul 30, 2015 at 14:33
  • I didn't know about printf, with printf it works :P ...so what does it make not working with echo then?
    – Dumbo
    Commented Jul 30, 2015 at 14:36
  • Is \r include in output or it's part of CRLF newline?
    – cuonglm
    Commented Jul 30, 2015 at 14:43
  • 11
    There are many different implementations of echo command incompatible with each other. Thus it is recommended to use more standard tool as printf. See unix.stackexchange.com/questions/65803/… for extended overview of the problem.
    – jimmij
    Commented Jul 30, 2015 at 14:44
  • 1
    In Stack Overflow, Echo newline in Bash prints literal \nEcho newline in Bash prints literal \n has some useful answers. Commented Apr 9, 2016 at 6:56

9 Answers 9


Pass the -e flag to echo to make it parse escaped characters such as "\r\n", like so:

echo -e "Line 1\r\nLine2" >> readme.txt
  • 2
    A nice simple "-e" option to get escape sequences working - it says on another answer that echo is unreliable and doesn't follow standards unlike printf.
    – Edward
    Commented Nov 22, 2017 at 22:44
  • 1
    Make sure your script doesn't contain any literal \r\n, such as in a tr -d "\n" command.
    – Noumenon
    Commented Nov 29, 2018 at 15:32


An echo implementation which strictly conforms to the Single Unix Specification will add newlines if you do:

echo 'line1\nline2'

But that is not a reliable behavior. In fact, there really isn't any standard behavior which you can expect of echo.



A string to be written to standard output. If the first operand is -n, or if any of the operands contain a <\backslash> character, the results are implementation-defined.

On XSI-conformant systems, if the first operand is -n, it shall be treated as a string, not an option. The following character sequences shall be recognized on XSI-conformant systems within any of the arguments:

\a - Write an <alert>.

\b - Write a <backspace>.

\c - Suppress the <newline> that otherwise follows the final argument in the output. All characters following the \c in the arguments shall be ignored.

\f - Write a <form-feed>.

\n - Write a <newline>.

\r - Write a <carriage-return>.

\t - Write a <tab>.

\v - Write a <vertical-tab>.

\\ - Write a <backslash> character.

\0num - Write an 8-bit value that is the zero, one, two, or three-digit octal number num.

And so there really isn't any general way to know how to write a newline with echo, except that you can generally rely on just doing echo to do so.

A bash shell typically does not conform to the specification, and handles the -n and other options, but even that is uncertain. You can do:

shopt -s xpg_echo
echo hey\\nthere


And not even that is necessary if bash has been built with the build-time option...


Make the echo builtin expand backslash-escaped characters by default, without requiring the -e option. This sets the default value of the xpg_echo shell option to on, which makes the Bash echo behave more like the version specified in the Single Unix Specification, version 3. See Bash Builtins, for a description of the escape sequences that echo recognizes.


On the other hand, printf's behavior is pretty tame in comparison.


The printf utility was added to provide functionality that has historically been provided by echo. However, due to irreconcilable differences in the various versions of echo extant, the version has few special features, leaving those to this new printf utility, which is based on one in the Ninth Edition system.

The EXTENDED DESCRIPTION section almost exactly matches the printf() function in the ISO C standard, although it is described in terms of the file format notation in XBD File Format Notation.

It handles format strings which describe its arguments - which can be any number of things, but for strings are pretty much either %byte strings or literal %strings. Other than the %formats in the first argument, it behaves most like a %byte string argument, except that it doesn't handle the \c escape.

printf ^%b%s$ '\n' '\n' '\t' '\t'

\n$^    \t$

See Why is printf better than echo? for more.

echo() printf

You might write your own standards conformant echo like...

    printf '%b ' "$@\n\c"

...which should pretty much always do the right thing automatically.

Actually, no... That prints a literal \n at the tail of the arguments if the last argument ends in an odd number of <backslashes>.

But this doesn't:

    case    ${IFS- } in
    (\ *)   printf  %b\\n "$*";;
    (*)     IFS=\ $IFS
            printf  %b\\n "$*"
  • +1 for the shopt -s xpg_echo option in echo to use -e by default. Commented Jun 8, 2018 at 13:54
  • This solution does't work without -e. I am surprised that no one noticed that except perhaps @HaydeSchiff
    – ha9u63a7
    Commented Oct 2, 2019 at 16:17
  • 1
    @ha9u63ar - uhh... did you read this?
    – mikeserv
    Commented Oct 3, 2019 at 22:55
  • When executing echo -e "package main\nversion = \"0.0.16\"" > version.go in a Makefile on Alpine 3.12 and make 4.3, it will write -e package main..., instead of package main.... It will not recognize -e as a flag and just print it. Use printf if you want your Makefiles run reliably. Commented Aug 13, 2020 at 7:49

In all versions of bash I've used, you may simply insert a literal newline into a string either interactively or in a script, provided that the string is sufficiently quoted.


$ echo "Line one
> Line two"
Line one
Line two

In a script:

echo "Line one
Line two"
echo -e "Line 1\r\nLine2" >> readme.txt

further usage possibilities:

man echo

People have already answered adequately, but I refuse to accept a world where echo accepts any option other than -n.

echo Line 1"$nl"Line 2"$nl"Line 3


echo ""

we can get empty new line i.e., double quotes without space.

  • Or just echo without parameter can be used in the same way Commented Mar 25 at 13:38

If you have a character that is not part of the message you can pipe through sed

$ echo "Time on the next line:@$(date +"%Y-%m-%dT%T%z")" | sed -e's/@/\n/g'
Time on the next line:

If you are trying to echo text containing exclamation marks (!) or single quotes (') you may run into some problems, therefore run:
echo -e $'text/with exclamation.mark!and\'single-quotes_here \n more\\\text'

so you don't have problems (make sure to still escape the one or more single quotes that are in the single quote string). Thanks to:



echo -e "line1\nline2"

This will print a new line.

  • 5
    This doesn’t add anything to the existing answers. Commented Feb 17, 2020 at 8:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .