在bash中隐藏错误消息 [英] Hide error message in bash

查看:119
本文介绍了在bash中隐藏错误消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在以下情况下,我无法从shell命令隐藏错误消息.

I have problem to hide error message from shell command as the following case.

firs_line=$(head -n 1 file) > /dev/null 2>&1

我希望错误消息将被隐藏,但实际上不会. head命令成功执行后如何获取输出,但失败时隐藏错误消息?

I expect that the error message will be hidden but actually it doesn't. How to get output while head command is executed successfully but hide error message when it fails?

谢谢.

推荐答案

错误消息是否来自head程序(例如,找不到文件)?

Is the error message coming from the head program (like, file not found)?

在这种情况下,您必须从内部解析器重定向输出:

In this case you have to redirect the output from inside parens:

firs_line=$(head -n 1 file 2>/dev/null)

此外,您只需要重定向标准错误(而不是重定向应该由$()捕获并存储在firs_line

Moreover, you only have to redirect standard error (and not standard output which is supposed to be catched by $() to be stored in firs_line

这篇关于在bash中隐藏错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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