Does the Exclamation point represent negation in Bash IF condition?

耗尽温柔 提交于 2021-01-27 05:23:32

问题


I've seen something along the lines of

if ! some-command; then
    #do something
fi

What's the effect of the exclamation point? I see that if you use brackets, it can be used for negation.

Is that the same effect here?


回答1:


As documented in man bash:

If the reserved word ! precedes a pipeline, the exit status of that pipeline is the logical negation of the exit status as described above.




回答2:


Correct.

Here is a code sample:

anny ~> if ! grep $USER /etc/passwd
More input> then echo "your user account is not managed locally"; fi
your user account is not managed locally

anny > echo $?
0

anny >

Source: http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_07_01.html



来源:https://stackoverflow.com/questions/41495350/does-the-exclamation-point-represent-negation-in-bash-if-condition

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!