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 ?


5 Answers 5


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.

  • 1
    Note that this is not POSIX, whether it is available largely depends on what shell you are using.
    – Chris Down
    Commented Mar 22, 2015 at 10:11
  • 1
    You find that construct in the prominent shells (at least), ksh, bash, zsh (and maybe others).
    – Janis
    Commented Mar 22, 2015 at 15:50
  • 2
    @ChrisDown, it's not POSIX yet, but scheduled for inclusion in issue 8. ksh93, bash, zsh, mksh, FreeBSD sh support it. Commented Feb 22, 2016 at 20:39

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
  • i 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 Commented Mar 21, 2015 at 23:33
  • You have to type EOF alone in one input line...
    – Rmano
    Commented Mar 21, 2015 at 23:38
  • interesting, 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 ? Commented Mar 22, 2015 at 0:00
  • 1
    @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 . Commented Mar 22, 2015 at 3:59
  • 1
    This worked for my Mac OSX (specifically echo -en 'text..', whereas the accepted answer didn't recognize my newline character (\n), the echo -en did. Commented Feb 3, 2017 at 15:29

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


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


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


POSIX 7 says you can't


-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 use printf instead:

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.

  • There 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 that which echo will not give you the path of the system echo in all shells. Commented Feb 22, 2016 at 20:43
  • @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? Commented Feb 22, 2016 at 20:46
  • Try 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. Commented Feb 22, 2016 at 20:49
  • printf 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.
    – i30817
    Commented Dec 12, 2021 at 17:13
  • 1
    @i30817 yes, gotta use printf '%s\n' Commented Dec 12, 2021 at 17:14

You must log in to answer this question.

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