In 4 types of bash if else statement Examples ( If then fi, If then else fi, If elif else fi, Nested if ), If-else is the unix script if decision making statements conditions that are evaluated by the simple program in bash scripting similar to any other VueJS, AngularJS, PHP, Laravel Examples. or any other programming Languages. Where execution or run of a block of code statement is certain based on the output of if bash if elsif statement condition. If it all the code evaluates a condition to return boolean value true, then if block code is successfully executed, on the boolean value return false condition, the else block code is successfully executed if in unix shell condition script, so which is optional.
Bash if else Statement in Bash Scripting
Syntax of the if else in bash
if [condition] then //if block code else // else block code fi
Types of if statements for bash if else
fundamentally, there are the following the list of four types of if statements.
- if statement
- if-else statement
- else-if ladder statement
- nested if statement
The Bash (Born Again) is a best way to learn Unix shell and CMD command line using in Terminal language written by the Brian Fox. The Born Again means a Bash shell is need by most of the usefult the Unix/Linux computer operating systems as there default run or execute the shell.
1. Bash – if Statement Example
Bash – if Statement is the basic or simple easy if condition, where if simple numeric or any other data types an expression run evaluates to return true a block of code is sucessfully executed.
For example, i have a select input of any number from the guest as well as simple check if the given users value is greater than 10 or not.
#!/bin/bash read -p "Put in numeric value: " userrandomnumber if [ $userrandomnumber -gt 10 ] then echo "Your Eneter Value is greater than 10" fi
2. Bash – if-else Statement Example
In our simple case the condition all the way to evaluates to return boolean value false then else block statements condition are executed Note : (if available).
Using the same source code as above. Only if a guest-entered value is greater than 10 then print “Good Luck, OK”. If the eneter your value equals to 10 or less then base simple display or print “Sorry, Not OK”
#!/bin/bash read -p "Put in Any numeric value: " userrandomnumber if [ $userrandomnumber -gt 10 ] then echo "Good Luck, OK" else echo "Sorry, Not OK" fi
3. Bash – If-elif-else Statement Example
In sum or included of the addition to else-if, we can check for fresh conditions, if the computing programme goes to else block code.
The elif (else if) is used for multiple or nested if conditions. In simple our case single if the condition goes return boolean value false then check second value if conditions. For simple example, your Enter the input the points of a student and check if points are greater or equal to 85 then print “great”. If points are less than 85 and greater or equal to 50 then print 50 and therefor on. Check and find status the below script as well as run or execute it on the shell with different any two inputs.
#!/bin/bash read -p "Put in your points: " points if [ $points -ge 85 ] then echo "great" elif [ $points -ge 55 ] then echo "Fint" elif [ $points -ge 30 ] then echo "Just Acceptable" else echo "sorry, Not OK or weak" fi
4. Bash – Nested if Statement Example
With multiple or nested if single condition goes return boolean value true then only check or find second condition. For simple example, I have take three numeric any values as input with the check which one is the greatest value.
#!/bin/bash read -p "Put/Enter in Guest value of first :" first read -p "Put/Enter in Guest value of second :" second read -p "Put/Enter in Guest value of third :" third if [ $first -gt $second ] then if [ $first -gt $third ] then echo "first is greatest" else echo "third is greatest" fi else if [ $second -gt $third ] then echo "second is greatest" else echo "third is greatest" fi fi
I hope you get an idea about 4 Bash If Statement Examples ( If then fi, If then else fi, If elif else fi, Nested if ).
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.
Here are some more FAQ related to this Article: