Testing
The testing commands shown here are usually used along with if/else
control flow statements. Both the string testing function ([[
) and the arithmetic testing function (((
) return 0 if the test evaluates to a true
value, or 1 if the test evaluates to false
. This is due to the 0 exit code of commands indicating success, and it is different from other programming languages you might know that typically evaluate a zero value as false. There is no native boolean
data type in Bash; the integers 0 and 1 are used in boolean contexts like this one. Sometimes, the variables true
and false
are initialized and used throughout a script.
Testing operators
Here are some basic boolean operators that you can use to construct statements in Bash – essentially, what you’re used to from other languages:
!
– not (negation)&&
– and||
– or
These operators can be used with both string and arithmetic test types:
...