Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Just looking for the difference between

  • 2>&-
  • 2>/dev/null
  • |&
  • &>/dev/null
  • >/dev/null 2>&1

and their portability with non-Bourne shells like tcsh, mksh, etc.

share|improve this question
Note that, while mksh supports &> for GNU bash compatibility, it’s strongly encouraged to not use this, as parsing it can break the semantics of existing POSIX scripts, and mksh disables that in POSIX mode already. – mirabilos Feb 27 '14 at 13:56
up vote 88 down vote accepted

For background:

  • a number 1 = standard out (i.e. STDOUT)
  • a number 2 = standard error (i.e. STDERR)
  • if a number isn't explicitly given, then number 1 is assumed by the shell (bash)

First let's tackle the function of these. For reference see the Advanced Bash-Scripting Guide.



The general form of this one is M>&-, where "M" is a file descriptor number. This will close output for whichever file descriptor is referenced, i.e. "M".


The general form of this one is M>/dev/null, where "M" is a file descriptor number. This will redirect the file descriptor, "M", to /dev/null.


The general form of this one is M>&N, where "M" & "N" are file descriptor numbers. It combines the output of file descriptors "M" and "N" into a single stream.


This is just an abbreviation for 2>&1 |. It was added in Bash 4.


This is just an abbreviation for >/dev/null 2>&1. It redirects file descriptor 2 (STDERR) and descriptor 1 (STDOUT) to /dev/null.


This is just an abbreviation for 1>/dev/null. It redirects file descriptor 1 (STDOUT) to /dev/null.

Portability to non-bash, tcsh, mksh, etc.

I've not dealt much with other shells outside of csh and tcsh. My experience with those 2 compared to bash's redirection operators, is that bash is superior in that regard. See the tcsh man page for more details.

Of the commands you asked about none are directly supported by csh/tcsh. You'd have to use different syntaxes to construct similar functions.

share|improve this answer
We have a winner. But so there's no performance difference or some such with 2>&- vs 2>/dev/null (other than that some "poorly" written programs don't undrestand 2>&- correctly)? – Det Apr 6 '13 at 14:38
There should be no performance difference. – slm Apr 6 '13 at 14:38
I'd vote that up if only I could. Lol. – Det Apr 6 '13 at 14:45
&> was in bash from the start (and breaks Bourne and POSIX compatibility as it means something different there, though is unlikely to be hit). >& and |& come from (t)csh (and it's their only way to redirect stderr). They were in zsh from the start and have only been added recently to bash. See also rc for better designed operators. – Stéphane Chazelas Oct 24 '14 at 9:26
Update: about the performance issue, that's also confirmed here: unix.stackexchange.com/questions/163955/… – Det Apr 16 at 14:24

This is for redirecting the STDERR & STDOUT:

  • 2>/dev/null

    Redirect STDERR to /dev/null (prevent from showing up on console)

  • |&

    Redirect STDERR and STDOUT to STDIN of piped command (cmd1 |& cmd2)

  • &>/dev/null

    Redirect both STDERR & STDOUT to /dev/null (nothing shows up on console)

  • >/dev/null

    Redirect STDOUT to /dev/null (only STDERR shows on console)

  • 2>&-

    Is for closing a file descriptor used with redirection

These are all standard redirection methods for the Bourne shells.

share|improve this answer
|& and &>/dev/null are not portable. – Chris Down Apr 3 '13 at 9:04

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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