>>
OperatorThe >>
operator allows us to append the standard output (stdout) of a command to the end of a file. The syntax is:
command >> file
We can also leave out the spaces. All three lines below do the same thing:
1 2 3 | command>>file
command >>file
command>> file
|
It is also possible to append a command's standard error to a file using the same operator.
If file
does not already exist, it will be created.
echo
To append "Hello, world!" to the end of the file myfile.log
, we can write:
echo "Hello, world!" >> myfile.log
Because the echo
command automatically adds a newline (line break) at the end of the text, this will add a newline to the end of the file as well.
If we want to append just the text without a newline at the end, we can use the echo
command's -n
option:
echo -n "Hello, world!" >> myfile.log
printf
An alternative to using echo
is to use the printf
command, which allows you to specify an output format using special character sequences like %s
for "insert text string here" and \n
for newline (line break).
The first argument of printf
is the string that specifies the output format. To indicate that we want to display a text string (%s
) followed by a newline (\n
), we can use the format string %s\n
. Then, we can supply the content of the text string as the next argument to printf
. Putting it together, to append Hello, world!
followed by a newline, we can run the following command:
printf "%s\n" "Hello, world!" >> myfile.log
If we don't want the newline at the end, we can simply leave it out of the format string:
printf %s "Hello, world!" >> myfile.log
To append the contents of file1.log
to the end of file2.log
:
cat file1.log >> file2.log
The cat
command prints the contents of file1.log
to standard output, and the >>
operator appends that output to the end of file2.log
.
cat
does not automatically add a newline at the end of file2.log
. To add a newline, we can combine it with printf
:
printf "%s\n" "$(cat file1.log)" >> file2.log
This tells Bash to first store the output of the command cat file1.log
to a string and then pass that string into the printf "%s\n"
command we saw earlier.
The above examples have all shown how to append standard output from a command (echo
, printf
and cat
) to a file. But what if you want to append standard error (stderr)? For that, we can specify standard error by its POSIX file descriptor number 2
, as shown below.
To append both standard output (which is always assigned file descriptor 1
in POSIX) and standard error (file descriptor 2
) from a command to a file, we can write:
command >>file 2>&1
This first tells Bash to append standard output from command
to file
(>> file
), and then tells it to redirect standard error to whereever standard output is going (i.e., append to file
).
Although standard output is implied when we write >>file
, we can also write 1
explicitly:
command 1>>file 2>&1
This example does the exact same thing as the previous example.
To append only standard error from a command to a file, we can write:
command 2>>file
Standard output will be left alone, so if we run this command from a terminal command line, standard output will show up on screen while standard error will be appended to file
.
To append standard output to out.log
and standard error to err.log
, we can write:
command 1>>out.log 2>>err.log
We can also leave out 1
since it is implied:
command >>out.log 2>>err.log