2

What is the purpose of & as in &> and &>> in bash?

1

2 Answers 2

3

They are both connected to I/O redirection as explained here.

In the case of &>, that redirects standard output (stdout) and standard error output (stderr) to a file. For example, let’s redirect the output of date to a file like this:

date &> foo.txt

That would create—or overwrite—a file named foo.txt with the output of date. And the contents of that file would be something like this:

Sun Oct  4 22:37:58 EDT 2015

But in the case of &>> that that redirects standard output (stdout) and standard error output (stderr) to a file. But the >> indicates that the file designated should be appended to and not overwritten if it already exists.

So if I ran this command a few times in a row:

date &> foo.txt

All foo.txt would ever have is the output of the last time that command was run since the single > indicates that the file content would be overwritten with each run of the command.

But if I run this command with the >> a few times in a row:

date &>> foo.txt

The foo.txt file would have the output of date appended to it like this:

Sun Oct  4 22:37:58 EDT 2015
Sun Oct  4 22:38:06 EDT 2015
Sun Oct  4 22:38:13 EDT 2015
Sun Oct  4 22:38:15 EDT 2015
Sun Oct  4 22:38:16 EDT 2015

Just note that &>> doesn’t appear to work in Bash 3.x, but works as expected in Bash 4.x.

0

Consider a command called cmd:

cmd &>filename

The above sends both the command's standard out and its standard error to filename, overwriting anything that was previously in that file. It is equivalent to:

cmd >filename 2>&1

The &>> form is very similar:

cmd &>>filename

This appends cmd's stdout and stderr to whatever was already in file filename. It is equivalent to:

cmd >>filename 2>&1

You can read more about this in man bash.

You must log in to answer this question.

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