普通输出发生在文件描述符 1(标准输出)上。诊断输出以及用户交互(提示等)发生在文件描述符 2(标准错误)上,输入进入文件描述符 0(标准输入)上的程序。
标准输出/错误的输出示例:
echo 'This goes to stdout'
echo 'This goes to stderr' >&2
Run Code Online (Sandbox Code Playgroud)
在上面的两个实例中,echo
写入标准输出,但在第二个命令中,命令的标准输出被重定向到标准错误。
过滤(删除)一个或另一个(或两个)输出通道的示例:
{
echo 'This goes to stdout'
echo 'This goes to stderr' >&2
} >/dev/null # stderr will still be let through
{
echo 'This goes to stdout'
echo 'This goes to stderr' >&2
} 2>/dev/null # stdout will still be let through
{
echo 'This goes to stdout'
echo 'This goes to stderr' >&2
} >/dev/null 2>&1 # neither stdout nor stderr will be let through
Run Code Online (Sandbox Code Playgroud)
输出流连接到当前终端(/dev/pts/0
在您的情况下),除非重定向到其他地方(如上所示)。