使用反引号时如何抑制命令输出?

时间:2013-09-13 13:20:11

标签: linux git bash backticks

如果我在shell脚本中使用反引号,有没有办法压缩git的命令输出? 这是我目前的代码:

OUT=$(git status > /dev/null)

谢谢:)

2 个答案:

答案 0 :(得分:7)

我认为你想要的是压制stderr而不是stdout,因为你仍然想要这个值。你可以这样做:

OUT=$(git status 2>/dev/null)

答案 1 :(得分:1)

如果某些输出出现标准错误:

OUT=$(git status > /dev/null 2>&1; echo $?)

当然,这确实留下了一个问题:你想在OUT中捕捉到什么?

[编辑] 以上内容会将git的返回码放入$OUT