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?

0

2 Answers 2

7

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

2 Comments

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.
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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.