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
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
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
echo
is unreliable and doesn't follow standards unlike printf
.
– Edward
Nov 22 '17 at 22:44
tr -d "\n"
command.
– Noumenon
Nov 29 '18 at 15:32
echo
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
.
string
<blockquote> <p>A string to be written to standard output. If the first operand is <code>-n</code>, or if any of the operands contain a <em><<code>\</code>backslash></em> character, the <strong><em>results are implementation-defined</em></strong>.</p> <p>On <a href="http://pubs.opengroup.org/onlinepubs/9699919799/help/codes.html#XSI" rel="noreferrer">XSI-conformant</a> systems, if the first operand is <code>-n</code>, it shall be treated as a string, <strong><em>not an option</em></strong>. The following character sequences shall be recognized on XSI-conformant systems within any of the arguments:</p> <blockquote> <p><code>\a</code> - Write an <em><alert></em>.</p> <p><code>\b</code> - Write a <em><backspace></em>.</p> <p><code>\c</code> - Suppress the <em><newline></em> that otherwise follows the final argument in the output. All characters following the <code>\c</code> in the arguments shall be ignored.</p> <p><code>\f</code> - Write a <em><form-feed></em>.</p> <p><code>\n</code> - Write a <em><newline></em>.</p> <p><code>\r</code> - Write a <em><carriage-return></em>.</p> <p><code>\t</code> - Write a <em><tab></em>.</p> <p><code>\v</code> - Write a <em><vertical-tab></em>.</p> <p><code>\\</code> - Write a <em><backslash></em> character.</p> <p><code>\0num</code> - Write an 8-bit value that is the zero, one, two, or three-digit octal number <em><code>num</code></em>.</p> </blockquote> </blockquote>
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
hey
there
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 thexpg_echo
shell option toon
, which makes the Bashecho
behave more like the version specified in the Single Unix Specification, version 3. See Bash Builtins, for a description of the escape sequences thatecho
recognizes.
printf
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 byecho
. However, due to irreconcilable differences in the various versions ofecho
extant, the version has few special features, leaving those to this newprintf
utility, which is based on one in the Ninth Edition system.<p>The EXTENDED DESCRIPTION section almost exactly matches the <a href="http://pubs.opengroup.org/onlinepubs/9699919799/functions/printf.html" rel="noreferrer"><em><code>printf()</code></em></a> function in the ISO C standard, although it is described in terms of the file format notation in <a href="http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap05.html#tag_05" rel="noreferrer">XBD File Format Notation</a>.</p>
It handles format strings which describe its arguments - which can be any number of things, but for strings are pretty much either %b
yte strings or literal %s
trings. Other than the %f
ormats in the first argument, it behaves most like a %b
yte 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...
echo(){
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:
echo()
case ${IFS- } in
(\ *) printf %b\\n "$*";;
(*) IFS=\ $IFS
printf %b\\n "$*"
IFS=${IFS#?}
esac
shopt -s xpg_echo
option in echo
to use -e
by default.
– Vignesh Raja
Jun 08 '18 at 13:54
-e
. I am surprised that no one noticed that except perhaps @HaydeSchiff
– ha9u63a7
Oct 02 '19 at 16:17
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.
– Ferdinand Prantl
Aug 13 '20 at 07: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.
Interactively:
$ echo "Line one
> Line two"
Line one
Line two
$
In a script:
echo "Line one
Line two"
People have already answered adequately, but I refuse to accept a world where echo accepts any option other than -n
.
nl='
'
echo Line 1"$nl"Line 2"$nl"Line 3
Using
echo ""
we can get empty new line i.e., double quotes without space.
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:
2016-03-25T10:12:39-0500
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
mandatory ? you can useprintf
. – Archemar Jul 30 '15 at 14:33\r
include in output or it's part ofCRLF
newline? – cuonglm Jul 30 '15 at 14:43echo
command incompatible with each other. Thus it is recommended to use more standard tool asprintf
. See http://unix.stackexchange.com/questions/65803/why-is-printf-better-than-echo for extended overview of the problem. – jimmij Jul 30 '15 at 14:44