How do I accomplish something like the following in Bash?
if ("$a" == "something" || ($n == 2 && "$b" == "something_else")); then ... fi
"&&" is used to chain commands together, such that the next command is run if and only if the preceding command exited without errors (or, more accurately, exits with a return code of 0). "-" is a command line argument with no specific bash function.
Bash AND Logical Operator Bash boolean AND operator takes two operands and returns true if both the operands are true, else it returns false.
To use multiple conditions in one if-else block, then elif keyword is used in shell. If expression1 is true then it executes statement 1 and 2, and this process continues. If none of the condition is true then it processes else part.
You almost got it:
if [[ "$a" == "something" || ($n == 2 && "$b" == "something_else") ]]; then
In fact, the parentheses can be left out because of operator precedence, so it might also be written as
if [[ "$a" == "something" || $n == 2 && "$b" == "something_else" ]]; then
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