markdown Bash文件描述符

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了markdown Bash文件描述符相关的知识,希望对你有一定的参考价值。

## 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](http://www.tldp.org/LDP/abs/html/io-redirection.html).

## 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`.

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](http://linux.die.net/man/1/tcsh) for more details.

以上是关于markdown Bash文件描述符的主要内容,如果未能解决你的问题,请参考以下文章

sh Bash:echo,exec,cat:附加文件描述符

markdown [Bash配置] bash文件配置供参考#bash #linux

Linux报错-bash: fork: retry: Resource temporarily unavailable和进程描述符之间的关系

Linux报错-bash: fork: retry: Resource temporarily unavailable和进程描述符之间的关系

在 Windows 上的 Ubuntu bash 上“找不到引用控制台的文件描述符”

sh Bash:echo,exec,cat:用于写入或截断模式的文件描述符