90

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 Answers5

102

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.

Janis
  • 14,222
78

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
Chris Down
  • 125,559
  • 25
  • 270
  • 266
  • 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 – Abdul Al Hazred Mar 21 '15 at 23:33
  • You have to type EOF alone in one input line... – Rmano Mar 21 '15 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 ? – Abdul Al Hazred Mar 22 '15 at 00: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: http://en.wikipedia.org/wiki/Here_document . – dave_thompson_085 Mar 22 '15 at 03: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. – benjaminmgross Feb 03 '17 at 15:29
13

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.

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

gardenhead
  • 2,017
1

POSIX 7 says you can't

http://pubs.opengroup.org/onlinepubs/9699919799/utilities/echo.html

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

Ciro Santilli OurBigBook.com
  • 18,092
  • 4
  • 117
  • 102