I want to ask is it possible to give multiple statements to be executed when if condition gets satisfied by doing just
if[condition] then statements else statements fi
or we have to do something else like using do.....done around that block of statements
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.
Bash else-if statement is used for multiple conditions. It is just like an addition to Bash if-else statement. In Bash elif, there can be several elif blocks with a boolean expression for each one of them. In the case of the first 'if statement', if a condition goes false, then the second 'if condition' is checked.
The keyword if is followed by a condition. This condition is evaluated to decide which statement will be executed by the processor. If the condition evaluates to TRUE, the processor will execute the statement(s) followed by the keyword then. In the syntax, it is mentioned as statement1.
Two types of conditional statements can be used in bash. These are, 'If' and 'case' statements. 'If' statements can be used to check the conditions in multiple ways.
Yes you can have multiple statements:
if [ condition ]; then echo 'foo' echo 'bar' else echo 'hello' echo 'world' fi
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