如何抑制命令错误信息? [英] How to suppress error message of a command?

查看:73
本文介绍了如何抑制命令错误信息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何隐藏shell命令的错误消息?

How can I suppress error messages for a shell command?

例如,如果目录中只有jpg个文件,则运行ls *.zip会给出错误消息:

For example, if there are only jpg files in a directory, running ls *.zip gives an error message:

   $ ls *.zip
   ls: cannot access '*.zip': No such file or directory

是否可以禁止显示此类错误消息?我想在bash脚本中使用此命令,但是我想隐藏所有错误.

Is there an option to suppress such error messages? I want to use this command in a bash script but I want to hide all errors.

推荐答案

大多数Unix命令(包括ls)会将常规输出写入stdout,并将错误消息写入stderr,因此您可以使用

Most Unix commands, including ls, will write regular output to stdout and error messages to stderr so you can use bash redirection to throw away the error messages while leaving the regular output in place:

ls *.zip 2> /dev/null

这篇关于如何抑制命令错误信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆