2

I think this is some kind of io redirect but it looks different to the examples from manual. Normally, we can redirect stdout of the script by following line:

exec 1> /var/log/a.log

But I saw this line in one of script:

exec &> /var/log/a.log

What's the meaning of '&' here? Is it both stdout and stderr?

Thanks, James

James Gan
  • 376
  • 1
  • 5
  • 10

1 Answers1

4

Your guess is right.

  • exec >filename command redirects stdout to the designated file. This sends all command output that would normally go to stdout to that file.
  • command &> filename redirects both the stdout and the stderr of command to filename.

Actually, command &>file is another form of command > file 2>&1 (not available in Bourne Shell prior to version 4, final release, or in the standard shell Debian Almquist shell used in Debian/Ubuntu):

What does the & charachter do? Very well explained here:

In shells derived from csh (the C shell), the syntax instead appends the & (ampersand) character to the redirect characters, thus achieving a similar result. The reason for this is to distinguish between a file named '1' and stdout, i.e. cat file 2>1 vs cat file 2>&1. In the first case, stderr is redirected to a file named '1' and in the second, stderr is redirected to stdout.

Read more on I/O redirection here:

http://www.tldp.org/LDP/abs/html/io-redirection.html

http://www.tldp.org/LDP/intro-linux/html/sect_05_02.html#sect_05_02_01

Diamond
  • 8,791
  • 3
  • 22
  • 37