bash ">/dev/null 2>&1" 是否有命令行快捷方式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/928667/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there a command-line shortcut for ">/dev/null 2>&1"
提问by
It's really annoying to type this whenever I don't want to see a program's output. I'd love to know if there is a shorter way to write:
每当我不想看到程序的输出时,输入这个真的很烦人。我很想知道是否有更短的写法:
$ program >/dev/null 2>&1
Generic shell is the best, but other shells would be interesting to know about too, especially bash or dash.
通用 shell 是最好的,但了解其他 shell 也会很有趣,尤其是 bash 或 dash。
回答by andrewdotn
>& /dev/null
回答by Ayman Hourieh
You can write a function for this:
您可以为此编写一个函数:
function nullify() {
"$@" >/dev/null 2>&1
}
To use this function:
要使用此功能:
nullify program arg1 arg2 ...
Of course, you can name the function whatever you want. It can be a single character for example.
当然,您可以随意命名该函数。例如,它可以是单个字符。
By the way, you can use execto redirect stdoutand stderrto /dev/nulltemporarily. I don't know if this is helpful in your case, but I thought of sharing it.
顺便说一句,你可以使用exec重定向stdout和stderr对/dev/null暂时的。我不知道这对你的情况是否有帮助,但我想分享它。
# Save stdout, stderr to file descriptors 6, 7 respectively.
exec 6>&1 7>&2
# Redirect stdout, stderr to /dev/null
exec 1>/dev/null 2>/dev/null
# Run program.
program arg1 arg2 ...
# Restore stdout, stderr.
exec 1>&6 2>&7
回答by Zaz
In bash, zsh, and dash:
在bash、zsh和dash 中:
$ program >&- 2>&-
It may also appearto work in other shells because &-is a bad file descriptor.
它也可能出现在其他shell工作,因为&-是一个坏文件描述符。
Note that this solution closes the file descriptors rather than redirecting them to /dev/null, which could potentially cause programs to abort.
请注意,此解决方案关闭文件描述符而不是将它们重定向到/dev/null,这可能会导致程序中止。
回答by swampsjohn
Most shells support aliases. For instance, in my .zshrc I have things like:
大多数 shell 支持别名。例如,在我的 .zshrc 中,我有以下内容:
alias -g no='2> /dev/null > /dev/null'
Then I just type
然后我只需输入
program no
回答by Reverent Lapwing
It's also worth noting, that often times redirecting output is not really necessary. Many Unix and Linux programs accept a "silent flag", usually -nor -q, that suppresses any output and only returns a value on success or failure.
还值得注意的是,很多时候重定向输出并不是真正必要的。许多 Unix 和 Linux 程序接受一个“静默标志”,通常是-n或-q,它会抑制任何输出并且只在成功或失败时返回一个值。
For example
例如
grep foo bar.txt >/dev/null 2>&1
if [ $? -eq 0 ]; then
do_something
fi
Can be rewritten as
可以改写为
grep -q foo bar.txt
if [ $? -eq 0 ]; then
do_something
fi
回答by loxaxs
Edit: the (:)or |:based solutions might cause an error because :doesn't read stdin. Though it might not be as bad as closing the file descriptor, as proposed in Zaz's answer.
编辑:基于(:)或|:的解决方案可能会导致错误,因为:没有读取stdin. 虽然它可能不像关闭文件描述符那么糟糕,正如 Zaz 的回答中所建议的那样。
For bash and bash-compliant shells (zsh...):
$ program &>/dev/null OR $ program &> >(:) # Should actually cause error or abortionFor all shells:
$ program 2>&1 >/dev/null OR $ program 2>&1|: # Should actually cause error or abortion$ program 2>&1 > >(:)does not work for dash because it refuses to operate process substitution right of a file substitution.
对于 bash 和 bash 兼容的 shell (zsh ...):
$ program &>/dev/null OR $ program &> >(:) # Should actually cause error or abortion对于所有壳:
$ program 2>&1 >/dev/null OR $ program 2>&1|: # Should actually cause error or abortion$ program 2>&1 > >(:)对 dash 不起作用,因为它拒绝操作文件替换的进程替换权。
Explanations:
说明:
2>&1redirects stderr (file descriptor 2) to stdout (file descriptor 1).|is the regular piping of stdout to the stdin of another command.:is a shell builtin which does nothing (it is equivalent totrue).&>redirects both stdout and stderr outputs to a file.>(your-command)is process substitution. It is replaced with a path to a special file, for instance:/proc/self/fd/6. This file is used as input file for the commandyour-command.
2>&1将 stderr(文件描述符 2)重定向到 stdout(文件描述符 1)。|是 stdout 到另一个命令的 stdin 的常规管道。:是一个 shell 内置函数,它什么都不做(相当于true)。&>将 stdout 和 stderr 输出重定向到一个文件。>(your-command)是过程替换。它被替换为特殊文件的路径,例如:/proc/self/fd/6. 该文件用作命令的输入文件your-command。
Note: A process trying to write to a closed file descriptor will get an EBADF(bad file descriptor) error which is more likely to cause abortion than trying to write to | true. The latter would cause an EPIPE(pipe) error, see Charles Duffy's comment.
注意:尝试写入关闭的文件描述符的进程将得到EBADF(错误的文件描述符)错误,这比尝试写入| true. 后者会导致EPIPE(管道)错误,请参阅 Charles Duffy 的评论。
回答by Greg Hewgill
If /dev/nullis too much to type, you could (as root) do something like:
如果输入/dev/null太多,您可以(以 root 身份)执行以下操作:
ln -s /dev/null /n
Then you could just do:
那么你可以这样做:
program >/n 2>&1
But of course, scripts you write in this way won't be portable to other systems without setting up that symlink first.
但是,当然,如果不先设置该符号链接,您以这种方式编写的脚本将无法移植到其他系统。
回答by Psychonaut
Ayman Hourieh's solutionworks well for one-off invocations of overly chatty programs. But if there's only a small set of commonly called programs for which you want to suppress output, consider silencing them by adding the following to your .bashrcfile (or the equivalent, if you use another shell):
Ayman Hourieh 的解决方案非常适合一次性调用过于繁琐的程序。但是,如果只有一小部分常用程序需要抑制输出,请考虑通过将以下内容添加到.bashrc文件中来使它们静音(如果您使用另一个 shell,则为等效项):
CHATTY_PROGRAMS=(okular firefox libreoffice kwrite)
for PROGRAM in "${CHATTY_PROGRAMS[@]}"
do
printf -v eval_str '%q() { command %q "$@" &>/dev/null; }' "$PROGRAM" "$PROGRAM"
eval "$eval_str"
done
This way you can continue to invoke programs using their usual names, but their stdout and stderr output will disappear into the bit bucket.
这样您就可以继续使用它们通常的名称调用程序,但是它们的 stdout 和 stderr 输出将消失在位桶中。
Note also that certain programs allow you to configure how much logging/debugging output they spew. For KDE applications, you can run kdebugdialogand selectively or globally disable debugging output.
另请注意,某些程序允许您配置它们输出的日志记录/调试输出量。对于 KDE 应用程序,您可以运行kdebugdialog并有选择地或全局地禁用调试输出。
回答by Maryam Jeddian
Seems to me, that the most portable solution, and best answer, would be a macro on your terminal (PC).
在我看来,最便携的解决方案和最佳答案是终端(PC)上的宏。
That way, no matter what server you log in to, it will always be there.
这样,无论您登录到哪个服务器,它都将始终存在。
If you happen to run Windows, you can get the desired outcome with AHK (google it, it's opensource) in two tiny lines of code. That can translate any string of keys into any other string of keys, in situ.
如果您碰巧运行 Windows,则可以在两行代码中使用 AHK(谷歌它,它是开源的)获得所需的结果。这可以就地将任何键串转换为任何其他键串。
You type "ugly.sh >>NULL" and it will rewrite it as "ugly.sh 2>&1 > /dev/null" or what not.
您键入“ugly.sh >>NULL”,它会将其重写为“ugly.sh 2>&1 > /dev/null”或其他什么内容。
Solutions for other platforms are somewhat more difficult. AppleScript can paste in keyboard presses, but can't be triggered that easily.
其他平台的解决方案稍微困难一些。AppleScript 可以粘贴键盘按键,但不能轻易触发。

