Skip to content

Instantly share code, notes, and snippets.

@ipan
Last active March 29, 2018 05:30
Show Gist options
  • Save ipan/d53e39c6f9c172f80adf3a5902e7f62f to your computer and use it in GitHub Desktop.
Save ipan/d53e39c6f9c172f80adf3a5902e7f62f to your computer and use it in GitHub Desktop.
bash redirect

bash redirect

Error redirect to /dev/null

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

For reference see the Advanced Bash-Scripting Guide.

  • 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)

Functions

2>&- 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".

2>/dev/null 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.

2>&1 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.

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

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

http://unix.stackexchange.com/questions/70963/difference-between-2-2-dev-null-dev-null-and-dev-null-21

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment