29

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 ?

27

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 Mar 22 '15 at 10:11
  • 1
    You find that construct in the prominent shells (at least), ksh, bash, zsh (and maybe others). – Janis Mar 22 '15 at 15:50
  • 1
    @ChrisDown, it's not POSIX yet, but scheduled for inclusion in issue 8. ksh93, bash, zsh, mksh, FreeBSD sh support it. – Stéphane Chazelas Feb 22 '16 at 20:39
24

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
  • 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
  • 2
    @mikeserv Are you being facetious or serious? – Matthew Read Mar 22 '15 at 0:15
  • 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 . – dave_thompson_085 Mar 22 '15 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. – benjaminmgross Feb 3 '17 at 15:29
4

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.

2

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

0

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.

  • 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. – Stéphane Chazelas Feb 22 '16 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? – Ciro Santilli 新疆改造中心996ICU六四事件 Feb 22 '16 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. – Stéphane Chazelas Feb 22 '16 at 20:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.