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?
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 !!!"
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 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 .
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With