Bash if else statement using Bash Scripting

linux Programming Technology

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.

List of all Google Adsense, VueJS, AngularJS, PHP, Laravel Examples.

if in unix shell script, bash if else, bash else if, if else in bash, if else bash, bash if elsif, bash if else statement, else if bash, bash if, else if in bash, bash script if else
Bash if else Statment

Bash if else Statement in Bash Scripting

Syntax of the if else in bash


if [condition]
    //if block code
   // else block code

Types of if statements for bash if else

fundamentally, there are the following the list of four types of if statements.

  1. if statement
  2. if-else statement
  3. else-if ladder statement
  4. nested if statement

Shell Scripting

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.

bash if elif, bash if else syntax, bash if example, if in unix shell script, if else statement bash, if then else bash, bash if condition, if statement bash, if in bash, bash if statements, bash elsif
Bash – if Statement Example

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.

read -p "Put in numeric value: " userrandomnumber
if [ $userrandomnumber -gt 10 ]
    echo "Your Eneter Value is greater than 10"

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).

if else bash script, if in unix shell script, bash elseif, bash if else elif, if then bash, bash elif, bash if then else, if else if bash, bash else, if statements in bash, bash if then
Bash – if-else Statement Example

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”

read -p "Put in Any numeric value: " userrandomnumber
if [ $userrandomnumber -gt 10 ]
    echo "Good Luck, OK"
    echo "Sorry, Not OK"

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.

if statement in bash, elseif bash, bash if fi, bash if else if, bash if else example, if in unix shell script, bash script if, bash if statement example, bash shell if else, if bash, linux bash if else
Bash – If-elif-else Statement Example

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.

read -p "Put in your points: " points
if [ $points -ge 85 ]
    echo "great"
elif [ $points -ge 55 ]
    echo "Fint"
elif [ $points -ge 30 ]
    echo "Just Acceptable"
    echo "sorry, Not OK or weak"

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.

shell script else if, if elif bash, else bash, bash if statement, bash if elif else, if statements bash, if else in shell script, bash if syntax, if bash script, bash script if statement, if in unix shell script
Bash – Nested if Statement Example

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 ]
    if [ $first -gt $third ]
        echo "first is greatest"
        echo "third is greatest"
    if [ $second -gt $third ]
        echo "second is greatest"
 echo "third is greatest"

Web Programming Tutorials Example with Demo

Read :


You can also read about AngularJS, ASP.NET, VueJs, PHP.

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 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.

Related FAQ

Here are some more FAQ related to this Article:

  1.   Validate Email and Password Fields on Form Submit Event
  2.   How to Check If Array is Empty in Laravel Blade?
  3.   ng click Event Function with Parameters using Angular Example
  4.   AngularJS Create Custom SEO Friendly URL Slugs
  5.   jquery Count Number of characters in string input
  6.   Simple Validations using Angular Example
  7.   Angular 8 Material Table Filter Sorting Paging
  8.   Validate Email Password Using jQuery
  9.   Ajax PHP MySQL Creating Autocomplete Search Suggestion

Leave a Reply

Your email address will not be published. Required fields are marked *