linux - 混淆输出重定向到 2>&1?

标签 linux bash shell ksh

我已经在 stackoverflow 和其他来源中检查了几个关于此的相关帖子,了解 2>&1 的使用。

不幸的是,到目前为止我还没有完全理解它。

我知道 2 是标准错误,1 是标准输出,我们正在结合 2>&1。

但我的问题是:

1. mycommand > /dev/null       
2. mycommand 2> /dev/null      
3. mycommand > /dev/null 2>&1  

我在想:

  1. 将 stdout 和 stderr 重定向到/dev/null
  2. 将 stderr 重定向到/dev/null
  3. 将 stdout 和 stderr 重定向到/dev/null

相关帖子:

最佳答案

看这个:

mycommand >/dev/null

它会将 mycommand 的 channel 1(即标准输出)重定向到/dev/null

mycommand 2>/dev/null

它会将 channel 2(即 stderr)重定向到/dev/null

mycommand >/dev/null 2>&1

它将 channel 1 重定向到/dev/null,然后将 channel 2 (stderr) 绑定(bind)到 channel 1 (stdout)。两者都将进入/dev/null

还有一个(刚刚完成)

mycommand 2>&1 >/dev/null

In this second case, I bind (the child's) stderr to stdout (of the parent) and then I find the child's stdout to /dev/null. The result is that you now get the child's stderr output on stdout and the stdout goes to the file. This is useful for processing stderr in a pipe, for example. (see this answer)

关于linux - 混淆输出重定向到 2>&1?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20494015/

相关文章:

linux - shell关闭后如何将nohup任务带到前台

php - 在网页中显示实时 wget 输出

c - 用C为linux写一个shell,某个进程的exec是无限循环的

arrays - Linux 控制台中的 MongoDB 命令带有 --eval (print

linux - 使用 vim 复制一个文件的内容并将其插入到另一个文件的特定行中

python - 如何在 Linux (virtualbox) 上启动并运行下载的包含许多文件的 python Web 应用程序

linux - 在 BASH 脚本中将日期插入到 "MAIL"消息正文中

c - 在实时应用程序中使用 malloc

linux - RedHat daemon函数使用

bash - 如何使用 grep 查找命令的输出