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.