How to put a newline special character into a file using the echo command and redirection operator?
Solution 1
You asked for using some syntax with the echo
command:
echo $'first line\nsecond line\nthirdline' > foo
(But consider also the other answer you got.)
The $'...'
construct expands embedded ANSI escape sequences.
Solution 2
What echo does with character escapes is implementation defined. In many implementations of echo (including most modern ones), the string passed is not examined for escapes at all by default.
With the echo provided by GNU bash (as a builtin), and some other echo variants, you can do something like the following:
echo -en 'first line\nsecond line\nthird line\n' > file
However, it really sounds like you want printf
, which is more legible to my eye, and more portable too (it has this feature defined by POSIX):
printf '%s\n' 'first line' 'second line' 'third line' > file
You also might consider using a here document:
cat > file << 'EOF'
first line
second line
third line
EOF
Solution 3
Here are some other ways to create a multi-line file using the echo
command:
echo "first line" > foo
echo "second line" >> foo
echo "third line" >> foo
where the second and third commands use the >>
redirection operator,
which causes the output of the command to be appended (added) to the file
(which should already exist, by this point).
Or
(echo "first line"; echo "second line"; echo "third line") > foo
where the parentheses group the echo
commands into one sub-process,
which looks and acts like any single program that outputs multiple lines
(like ls
, for example).
A subtle variation on the above is
{ echo "first line"; echo "second line"; echo "third line";} > foo
This is slightly more efficient than the second answer in that
it doesn't create a sub-process. However, the syntax is slightly trickier:
note that you must have a space after the {
and a semicolon before the }
.
See What are the shell's control and redirection operators? for more information.
Solution 4
To give yet another option, you can just hit Enter (i.e., a literal newline):
$ echo "the rain in spain
> falls mainly on the plain" > foo
Note that you don't type the >
at the beginning of the second line.
That's the secondary shell prompt, $PS2
,
which the shell prints when you type an incomplete command
(e.g., in this case, a command with an unmatched quote character).
Related videos on Youtube
Abdul Al Hazred
Updated on September 18, 2022Comments
-
Abdul Al Hazred almost 2 years
I would like to create a file by using the echo command and the redirection operator, the file should be made of a few lines.
I tried to include a newline by "\n" inside the string:
echo "first line\nsecond line\nthirdline\n" > foo
but this way no file with three lines is created but a file with only one line and the verbatim content of the string.
How can I create using only this command a file with several lines ?
-
Admin over 8 years
-
-
Abdul Al Hazred over 9 yearsi tried your cat > file << 'EOF' in the shell, an when i hit enter, i got this > shaped prompt with each line, but i could not get out of it even by hitting ctrl+x or ctrl+z
-
Rmano over 9 yearsYou have to type EOF alone in one input line...
-
Abdul Al Hazred over 9 yearsinteresting, though I do not know how this structure works, first time i met it, i tried 'ficken' instead of 'EOF' and it works, too , but i have to use the other word than. cat > myfile << 'ficken' . Is there some keyword regarding this exotic structure to read more about ?
-
dave_thompson_085 over 9 years@AbdulAlHazred as the link in the answer indicates, it's called a "here document" and is very common. Wikipedia is good on this topic: en.wikipedia.org/wiki/Here_document .
-
clerksx over 9 yearsNote that this is not POSIX, whether it is available largely depends on what shell you are using.
-
Janis over 9 yearsYou find that construct in the prominent shells (at least), ksh, bash, zsh (and maybe others).
-
Stéphane Chazelas over 8 years@ChrisDown, it's not POSIX yet, but scheduled for inclusion in issue 8. ksh93, bash, zsh, mksh, FreeBSD sh support it.
-
Stéphane Chazelas over 8 yearsThere will be a
echo
builtin in every shell available on the system. On Ubuntu /bin/sh is usually dash, bash also comes by default. zsh, rc, mksh, yash, csh, tcsh, ksh93... will all have their own echo with different behaviour. Note thatwhich echo
will not give you the path of the system echo in all shells. -
Ciro Santilli Путлер Капут 六四事 over 8 years@StéphaneChazelas "which echo will not give you the path of the system echo in all shells." That I didn't know, what else could it give?
-
Stéphane Chazelas over 8 yearsTry in tcsh or zsh.
which
was originally (in the 80s) a csh script for csh users only (loaded the aliases from ~/.cshrc) and still is on some systems.which
is a builtin in zsh and tcsh. -
benjaminmgross over 7 yearsThis worked for my Mac OSX (specifically
echo -en 'text..'
, whereas the accepted answer didn't recognize my newline character (\n
), theecho -en
did. -
i30817 over 2 yearsprintf will break if the text has % on it (probably among others) and the 'bash support to escape special characters' to avoid that prints the escape to the file, which you obviously don't want.
-
Ciro Santilli Путлер Капут 六四事 over 2 years@i30817 yes, gotta use
printf '%s\n'
-
i30817 over 2 yearsSigh. I guess. I knew i was doing something wrong.
-
Ciro Santilli Путлер Капут 六四事 over 2 years@i30817 yes, the infinite joys of Bash.