Background
I recently came across this question on the Stackexchange site Unix & Linux. This question was interesting in the sense that it covered much of the I/O redirection facilities that are available in the Bash Shell, so for posterity sake I’m adding my answer to this question here on my blog.
Solution
- 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.
Functions
| - 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 2>&1 >/dev/null . It too was added in Bash 4. 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 asked about in the Unix & Linux question, none are directly supported by csh
or tcsh
. You’d have to use different syntaxes to construct similar functions.
References
- Advanced Bash-Scripting Guide
- tcsh man page
- stackexchange unix & linux question regarding shell redirections
NOTE: For further details regarding my one-liner blog posts, check out my one-liner style guide primer.