Conditional Statements: There are total 5 conditional statements which can be used in bash programming
- if statement
- if-else statement
- if..elif..else..fi statement (Else If ladder)
- if..then..else..if..then..fi..fi..(Nested if)
- switch statement
Their description with syntax is as follows:
This block will process if specified condition is true.
if [ expression ] then statement fi
If specified condition is not true in if part then else part will be execute.
if [ expression ] then statement1 else statement2 fi
if..elif..else..fi statement (Else If ladder)
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.
if [ expression1 ] then statement1 statement2 . elif [ expression2 ] then statement3 statement4 . else statement5 fi
Nested if-else block can be used when, one condition is satisfies then it again checks another condition. In the syntax, if expression1 is false then it processes else part, and again expression2 will be check.
if [ expression1 ] then statement1 statement2 . else if [ expression2 ] then statement3 . fi fi
case statement works as a switch statement if specified value match with the pattern then it will execute a block of that particular pattern
When a match is found all of the associated statements until the double semicolon (;;) is executed.
A case will be terminated when the last command is executed.
If there is no match, the exit status of the case is zero.
case in Pattern 1) Statement 1;; Pattern n) Statement n;; esac
$bash -f main.sh a is not equal to b
$bash -f main.sh a is equal to b
$bash -f main.sh Headquarters - Chennai, Tamil Nadu, India.
Note: Shell scripting is a case-sensitive language, which means proper syntax has to be followed while writing the scripts.
- Looping Statements | Shell Script
- String Operators | Shell Script
- Bash shell script to swap two numbers
- Bash shell script to find sum of digits
- Implementing Directory Management using Shell Script
- Automated Recursive Encryption in a Directory Using Shell Script
- Bash shell script to find out the largest value from given command line arguments
- Introduction to Linux Shell and Shell Scripting
- Making your own Linux Shell in C
- SHELL On-Campus Recruitment for SDE
- Developing a Linux based shell
- Basic Shell Commands in Linux
- Shell Interview Experience | On-Campus
- Shell India Interview Experience (IT)
- A Shell program To Find The GCD | Linux
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.