5

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?

Community
  • 1
  • 1
JuanCaicedo
  • 3,132
  • 2
  • 14
  • 36

2 Answers2

6

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.

choroba
  • 231,213
  • 25
  • 204
  • 289
1

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

Philip Raath
  • 460
  • 3
  • 18
  • `echo $?` will always return `0` because the exit status of the `if...then...fi` block is 0. Try it with different combination and you will see that it will always be 0 even when the `if` statement is broken, such as syntax errors. – alvits Jan 05 '17 at 23:55
  • 2
    In short, the exit status of the `if...fi` block is the exit status of the list within the block, not the exit status of the condition. – alvits Jan 06 '17 at 00:02