Jay Taylor's notes
back to listing indexbash - What does >& mean? - Stack Overflow
[web search]-
Home
-
- Public
-
Questions -
Tags
-
Users
-
Companies
-
Collectives
-
Explore Collectives
-
TeamsStack Overflow for Teams – Start collaborating and sharing organizational knowledge.
Create a free Team Why Teams?
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?
4 Answers
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
-
I feel foolish. I spent a bunch of time reading other sources and it was right there in the manpage. Jun 29, 2012 at 3:32
-
>&
is the syntax used by csh and tcsh to redirect both stdout and stderr. That's probably why bash accepts it. Jun 29, 2012 at 5:46 -
Does this mean that
&>word
and>word 2>&1
are semantically equivalent? The antecedent to "This" is not clear to me. Mar 11, 2015 at 16:04 -
-
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
) Mar 11, 2015 at 17:53
&>
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 inbash
. zsh
allows both&>>
and>>&
forms.
-
so is
&
a special character that gets converted to 1 and 2 by the shell interpreter? Oct 13, 2019 at 11:06 -
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.
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
)
Your Answer
Not the answer you're looking for? Browse other questions tagged bash io file-descriptor io-redirection or ask your own question.
- The Overflow Blog
-
-
- Featured on Meta
-
-
-
-
-
- Hot Meta Posts
-
59
Related
Hot Network Questions
-
Could someone help me to derive the closed form for this sum?
-
Find the nth Mersenne Prime
-
What's the story behind kobolds being little lizards?
-
Difference between skipeval and dimeval
-
If saying 'Why can't I ...?' is correct, would 'Why cannot I ...?' be technically correct?
-
Is URL rewriting in e-mail a sound security practice?
-
Do we need to cite articles to the paper from the journal to where we are sending to?
-
What is the point of the line on a check?
-
A 1-D labyrinth
-
Crystal oscillator with inverter gate, crystal engraved with "1 MHz" outputs 1.68 MHz
-
Meaning of "shade" in "a shabby green shade shoved up from one of his eyes"
-
What's this stuff that looks like white chainmail armor growing on giant kelp?
-
How bad would a leaked tax return be?
-
Does the SLS mobile launch platform share any hardware with STS or even Saturn I/V MLP?
-
Component that responds to amperage
-
Is Anchor a language or a framework?
-
In Chopin Marche funèbre, measure 19, on right hand, is the A flat played once or three times?
-
What gown do I wear to student graduation when I have two doctoral degrees?
-
How to create intervals of 100 based on a column using PLSQL
-
Sum of partition numbers
-
Finding inverse of an algebraic function and using ListLinePlot
-
Should I cite the paper from which I got the research question in my own paper?
-
Is possible to have UserInfo.getUserId() as null?
-
What are all of the non-solo types in Pokemon games?