Jay Taylor's notes

back to listing index

bash - What does >& mean? - Stack Overflow

[web search]
Original source (stackoverflow.com)
Tags: bash shell-scripting redirection stackoverflow.com
Clipped on: 2022-09-04

  1. Home
    1. Public
    2. Questions
    3. Tags
    4. Users
    5. Companies
    6. Collectives
    7. Explore Collectives
    1. Teams
      Stack Overflow for Teams – Start collaborating and sharing organizational knowledge. Create a free Team Why Teams?
Asked 10 years, 2 months ago
Modified 6 days ago
Viewed 49k times
110

I was a little confused by this expression:

gcc -c -g program.c >& compiler.txt

I know &>filename will redirect both stdout and stderr to file filename. But in this case the ampersand is after the greater than sign. It looks like it's of the form M>&N, where M and N are file descriptors.

In the snippet above, do M=1 and N='compiler.txt'? How exactly is this different from:

gcc -c -g program.c > compiler.txt     (ampersand removed)

My understanding is that each open file is associated with a file descriptor greater than 2. Is this correct?

If so, is a file name interchangeable with its file descriptor as the target of redirection?

Matthias Braun
29.2k1818 gold badges137137 silver badges164164 bronze badges
asked Jun 29, 2012 at 2:48
contrapositive
1,30122 gold badges1010 silver badges1111 bronze badges

4 Answers

Sorted by:
123

This is the same as &>. From the bash manpage:

Redirecting Standard Output and Standard Error This construct allows both the standard output (file descriptor 1) and the standard error output (file descriptor 2) to be redirected to the file whose name is the expansion of word.

There are two formats for  redirecting  standard  output  and  standard
error:

       &>word
and
       >&word

Of the two forms, the first is preferred.  This is semantically equiva-
lent to

       >word 2>&1
Denilson Sá Maia
45.1k3232 gold badges103103 silver badges111111 bronze badges
answered Jun 29, 2012 at 2:57
jordanm
30.5k77 gold badges6060 silver badges7272 bronze badges
23

&> vs >&: the preferred version is &> (clobber)

Regarding:

  • &>
  • >&

both will clobber the file - truncate the file to 0 bytes before writing to it, just like > file would do in the STDIN-only case.

However, the bash manual Redirections section adds that:

Of the two forms, the first is preferred. This is semantically equivalent to

>word 2>&1

When using the second form, word may not expand to a number or -. If it does, other redirection operators apply (see Duplicating File Descriptors below) for compatibility reasons.

(Note: in zsh both are equivalent.)

It's very good practice to train your fingers to use the first (&>) form, because:

Use &>> as >>& is not supported by bash (append)

There's only one append form:

The format for appending standard output and standard error is:

&>>word

This is semantically equivalent to

>>word 2>&1

(see Duplicating File Descriptors below).

Note:

  • The clobber usage of &> over >& in the section above is again recommended given that there is only one way for appending in bash.
  • zsh allows both &>> and >>& forms.
Matthias Braun
29.2k1818 gold badges137137 silver badges164164 bronze badges
answered Jun 30, 2018 at 6:39
Tom Hale
34.9k2727 gold badges161161 silver badges222222 bronze badges
1

Slightly off-topic, but this is why I stick to the long form like >/dev/null 2>&1 all the time. That is confusing enough for people who do it backwards in things like cron without adding >&, &>, >>&, &>>, ... on top of it. I frequently need to add notes in crontab -l reminding people that "2>&1 > /dev/null" is not a best practice.

As long as you remember that any "final destination" is given first, then it should be the same syntax on any Unix or Unix-like system using any Bourne-like shell, appending or otherwise. Plus, since &> is effectively a Bash-ism (may have originated in csh? don't recall, but it is not POSIX either way), it is not always supported on locked-down commercial UNIX systems.

answered Aug 26 at 22:51
Kajukenbo
8911 silver badge44 bronze badges
0
This post is hidden. It was deleted 7 years ago by the post author.

Now I realize that I made a mistake before based on what I read elsewhere. > only redirects standard out. > does not redirect error. (I should probably redirect my future comments to /dev/null)

answered Mar 11, 2015 at 17:53
geneorama
3,42544 gold badges2727 silver badges3737 bronze badges
Comments disabled on deleted / locked posts / reviews

Your Answer

Community wiki

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

Hot Network Questions