Software & Apps Linux How to Write IF-Statements in a Bash-Script Commands, Syntax, and Examples Share Pin Email Print Linux Switching from Windows By Juergen Haas Writer Former Lifewire writer Juergen Haas is a software developer, data scientist, and a fan of the Linux operating system. our editorial process Juergen Haas Updated January 30, 2020 With an if-statement, which is a type of conditional statement, you can perform different actions depending on specified conditions. It effectively gives the system the ability to make decisions. An example of the simplest form of an if-statement would be: count=5if [ $count == 5 ]then echo "$count"fi In this example, the variable "count" is used to specify a condition that is used as part of the if-statement. Before the if-statement is executed, the variable "count" is assigned the value "5". The if-statement then checks whether the value of "count" is "5". If that is the case the statement between the keywords "then" and "fi" are executed, otherwise any statements following the if-statement are executed. The keyword "fi" is "if" spelled backward. The bash scripting language uses this convention to mark the end of a complex expression, such an if-statement or case-statements. The "echo" statement prints its argument, in this case, the value of the variable "count", to the terminal window. Indentation of the code between the keywords of the if-statement improves readability but is not necessary. If you have a situation where a piece of code should be executed only if a condition is not true, you can use the keyword "else" in an if-statement, as in this example: count=5if [ $count == 5 ]then echo "$count"else echo "count is not 5"fi If the condition "$count == 5" is true, the system prints the value of the variable "count", otherwise it prints the string "count is not 5". If you want to differentiate between multiple conditions, you can use the keyword "elif", which is derived from "else if", as in this example: if [ $count == 5 ]then echo "count is five"elif [ $count == 6 ]then echo "count is six"elseecho "none of the above"fi If "count" is "5", the system prints out "count is five". If "count" is not "5" but "6", the system prints "count is six". If it is neither "5" nor "6", the system prints "none of the above". As you may have guessed, you can have any number of "elif" clauses. An example with multiple "elif" conditions would be: if [ $count == 5 ]then echo "count is five"elif [ $count == 6 ]then echo "count is six"elif [ $count == 7 ]then echo "count is seven"elif [ $count == 8 ]then echo "count is eight"elif [ $count == 9 ]then echo "count is nine"else echo "none of the above"fi A more compact way to write such statements with multiple conditions is the case method. It functions similar to the if-statement with multiple "elif" clauses but is more concise. For example, the above piece of code can be re-written with the "case" statement as follows: case "$count" in5)echo "count is five";;6)echo "count is six";;7)echo "count is seven";;8)echo "count is eight";;9)echo "count is nine";;*)echo "none of the above"esac If-statements are often used inside for-loops or while-loops as in this example: count=1done=0while [ $count -le 9 ]do sleep 1 (( count++ )) if [ $count == 5 ] then continue fi echo "$count"doneecho Finished You can also have nested if statements. Simplest nested if statement is of the form: if...then...else...if...then...fi...fi. However, if-statement can nested with arbitrary complexity. See also how to pass arguments to a bash script, which shows how to use conditionals to process parameters passed from the command line. The bash shell provides other programming constructs, such as for-loops, while-loops, and arithmetic expressions.