Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does the Exclamation point represent negation in Bash IF condition?

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?

like image 790
JuanCaicedo Avatar asked Jan 05 '17 21:01

JuanCaicedo


People also ask

What does the exclamation point mean in bash?

In Bash, the exclamation mark (!) is used with the pound (#) symbol to specify the interpreter path. This usage is called “shebang” and is denoted as: #!interpreter [arguments] In shell scripts, we can use it to specify bash as an interpreter: $ cat welcome.sh #!/usr/bin/bash echo "Welcome !!!"

What does exclamation mark mean in if statement?

By putting a single exclamation mark before a statement, you reverse the boolean. For example, ! true would equal false and ! false will equal true .

How do you negate if statements in bash?

How to negate an if condition in a Bash if statement? (if not command or if not equal) To negate any condition, use the ! operator, for example: if ! <test-command>; then <command-on-failure>; fi .

How do you negate a condition in shell script?

NegationWhen we use the not operator outside the [[, then it will execute the expression(s) inside [[ and negate the result. If the value of num equals 0, the expression returns true. But it's negated since we have used the not operator outside the double square brackets.


1 Answers

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.

like image 200
choroba Avatar answered Sep 27 '22 22:09

choroba