Looking for a Bash scripting tutorial to learn various bash statements? Here’s the detailed tutorial on Bash If statements, let’s dive in!

Bash If statements are quite useful to help you adapt the ways using which you can automate specific tasks. Bash If and Bash if-else statements allow people to make decisions within the bash scripts. Those statements put up a decision of whether to run a code or not based upon the condition set upon the statements.

The Bash If statements are often combined with certain loops to help you prepare complex scripts for solving large tasks. Even though their syntax is simple, they are used in diverse conditions across the Bash Scripting world. And, this article aims to cover the maximum possible Bash If statements for you.

Bash If and Bash If-Else Statements upon Diverse Conditions

The difference between Bash If and Bash If-else syntax is very minimal but can change the entire usability of the bash script. With Bash If, the statement will be executed only if the condition is true. In case the condition is false, then the statement will be ignored. But, with Bash If-else, there are two statements specified within the bash scripts for one condition. If the condition is true, then the first statement will be executed, but if the condition is false, then the second statement will be executed. Hence, now you know that the Bash conditions are responsible for executing the If and If-else statements.

If you intend to use Bash for scripting purposes, then you cannot deny the use of numerous conditions. At times, some conditions have complex syntaxes that make it difficult for beginners to understand. Therefore, here is the syntax and a brief explanation of the most commonly used Bash If statements:

  1. If [ $foo -ge 3]; then

This condition mentioned within the example is a command. Surrounding a comparison with the use of square brackets is the same as using test commands. In this condition, if ‘$foo’ is equal to or greater than 3, the statement after ‘then’ will execute.  

  1. if test $foo -ge 3; then

It is the same condition common as the first one, without square brackets, that makes it necessary for the use of test commands.

  1. if [ -f regularfile ]; then

This condition is true only if the file with name ‘regularfile’ exists and is actually a regular file. A regular file should not be a character/block device or a directory.

  1. if [ -r readablefile ]; then

The above condition is counted as true if the file with name ‘readablefile’ exists and is actually readable.

  1. if <condition>; then <commands> fi

It is the basic syntax of Bash If.

  1. if [ -r somefile ]; then | content=$ (cat somefile) elif

If this condition is true, only if there is a readable file with the name ‘somefile.’ If it is available, then the condition will read into the variable. But, if not, then the program will proceed to check if the file actually exists by compiling the second statement.

  1. if [ “stringvar” == “tux” ]; then

This statement implies the use of quotes for using strings in your conditions with the bash if statements.

  1. if [ ! -f regularfile ]; then

This statement implies that you can invert the purpose of the condition by adding an exclamation or ‘!’ mark within the brackets.

  1. if [ $foo -ge 3 -a $foo -lt 10]; then

This statement implies a combination of the conditions with the use of a single bracket syntax that uses ‘-a’ for AND & ‘-o’ for OR. This condition will turn out to be true only if the $foo consists of an integer that is equal to or greater than 3 and is less than 10. There are numerous combinations that you can try with such syntax!

  1. if [ -L symboliclink ]; then

It is a file-based condition that allows you to run different checks upon a file. This condition is true only if the symbolic link file with name ‘symboliclink’ in this condition exists.

  1. if [ -z “$emptystring” ]; then

It is a string-based condition, and this statement allows you to check a string or compare multiple strings. This condition is true only if the $emptystring is an uninitialized variable or is an empty string.

  1. if [ “$stringvar1” == “cheese” ]; then

It is also another type of string-based condition, and it is true if the ‘$stringvar1’ input consists of ‘cheese’ as its string.

  1. if [ $num -lt 1 ]; then

It is the arithmetic or number-based conditions, and this statement allows comparison of integer numbers. This condition is counted as true only if the entered number is less than 1.

  1. if [[ “$stringvar” == *string* ]]; then

It is the double-bracket syntax for Bash If statements. It serves as the enhanced version and technically has similar features but with certain differences. The double bracket syntax intends to feature ‘shell globbing.’ Moreover, this condition will be true if $stringvar has a phrase ‘string.’

  1. if [[ “$stringvar” == *[sS]tring* ]]; then

This condition is using diverse shell globbing techniques. Referring to this example condition, with shell globbing, you can check or match both ‘String’ and ‘string’ using this syntax. The condition will be true even if ‘$stringvar’ has ‘String’ or ‘string.’

  1. if [ -a *.sh ]; then

This condition explains the single bracket syntax for executing the file specified within the conditions. This condition will be true if there is only one file within the directory that consists of an extension ‘.sh.’ In case there are several ‘.sh’ files within the directory, bash will put up an error message and will stop the execution. It is because the *.sh command is expanded onto the files within a directory, and this can be prevented with the use of a double bracket.

  1. if [[ -a *.sh ]]; then

This condition explains the double-bracket syntax to avoid expanding the filename within conditions. This condition will be true if a file is present in the directory with extension ‘*.sh.’ It will execute and display the file, irrespective of whether there are other .sh files or not. The asterisk sign is quite important within the double bracket syntax to avoid expansion of the file names within double-bracket syntax.

  1. if [[ $num -eq 3 && “$stringvar” == foo ]]; then

This condition explains the use of combining operators ‘&&’ and ‘||.’ This condition has used ‘&&” that says that the statement will be true only if ‘$num’ is equal to 3 and the ‘$stringvar’ is equal to string ‘foo’. The AND and OR operators, ‘-a’ and ‘-o’ is also supported. But, the ‘&&’ and ‘||’ are mostly preferred for statements with double-bracket syntax. The AND operators will always be executed first over the OR operators.

  1. if (( $num <= 5 )); then

It is the double-parenthesis syntax, meant especially for the arithmetic conditions or number-based conditions. This condition is considered true if ‘$num’ is less than/equal to 5. This syntax is way more common for programmers over time. It uses some of the most commonly used operators such as “<, >=, and ==. This syntax will support the use of ‘&&’ and ‘||,’ but you cannot use ‘-a’ and ‘-o’ on it. The use of this syntax is equivalent to ‘Let’ command within bash.

File Based Bash If Conditions

  1. if [ -a existingfile ]; then

This condition is counted as true if the file with the name ‘existingfile’ exists within the directory.

  1. if [ -b blockspecialfile ]; then

The condition is true if the block special file with the name ‘blockspecialfile’ exists within the working directory.

  1. if [ -c characterspecialfile ]; then

The condition is true only if the character special file with the name ‘characterspecialfile’ is available within the search directory.

  1. if [ -d directory ]; then

The condition is true; if the file name ‘directory’ exists but is conditioned, it should actually be a directory file.

  1. if [ -e existingfile ]; then

If the file named ‘existingfile’ exists within the directory, then this condition will execute as true.

  1. if [ -g sgidfile ]; then

If the file with the name ‘sgidfile’ is a set-group-Id and exists within the search directory, then this condition will execute as true.

  1. if [ -G fileownedbyeffectivegroup ]; then

This condition is true if the file named ‘fileownedbyeffectivegroup’ is available and is completely owned by the group ID.

  1. if [ -k stickyfile ]; then

If the file named ‘stickyfile’ belongs to the sticky bit set and is available within the directory, then the condition is true.

  1. if [ -N modifiedsincelastread ]; then

The condition is true only if the file with name ‘modifiedsincelastread’ exists and was actually modified after it was read last.

  1. if [ -O fileownedbyeffectiveuser ]; then

The condition is termed to be true if the file with name ‘fileownedbyeffectiveuser’ is owned by the effective user and is available within the directory.

  1. if [ -p namedpipe ]; then

The file ‘namedpipe’ should be a named pipe and must be available within the directory for this condition to be true.

  1. if [ -s nonemptyfile ]; then

If the file with name ‘nonemptyfile’ is of size more than 0 bytes and is available within the directory, then the condition is true.

  1. if [ -S socket ]; then

A socket file within the directory with file name ‘socket’ can make this condition true.

  1. if [ newerfile -nt olderfile ]; then

The condition is true if the file with name ‘newerfile’ was modified recently as compared to ‘olderfile.’ It is also true if the ‘olderfile’ doesn’t exist and ‘newerfile’ does.

  1. if [ olderfile -ot newerfile ]; then

The condition is true if the file with name ‘olderfile’ was modified long ago than ‘newerfile.’ It is also true if the ‘newerfile’ is present in the directory and ‘olderfile’ doesn’t exist.

Whizlabs Bash Scripting Fundamentals course intends to help you learn how to create shell scripts using Bash to automate tasks on a UNIX system. Check it out now!

String-Based Bash If Conditions

  1. if [ STRING1 == STRING2 ]; then

The condition is true if ‘STRING1’ is entirely same as that of ‘STRING2’.

  1. if [ STRING1 != STRING2 ]; then

The condition is true if ‘STRING1’ is not same as that of ‘STRING2’.

  1. if [ STRING1 > STRING2 ]; then

The condition is true if the sorting of ‘STRING1’ is executed after ‘STRING2’ within the current locale.

  1. if [ STRING1 < STRING2 ]; then

The condition is true if the sorting of ‘STRING1’ is executed before ‘STRING2’, within the current locale.

  1. if [ -n NONEMPTYSTRING ]; then

The condition is true if the string named ‘NONEMPTYSTRING’ has a length more than 0 (ZERO).

  1. if [ -z EMPTYSTRING ]; then

The condition is true only if the string named ‘EMPTYSTRING’ is of Zero-length.

  1. if [[ STRING1 =~ REGEXPATTERN ]]; then

This statement works only with double-bracket syntax. This condition is true only if the string named ‘STRING1’ matches with the REGEXPPATTERN.

Arithmetic or Number-Based Bash If Conditions

  1. if [ NUM1 -eq NUM2 ]; then

The condition is true if ‘NUM1’ is same as ‘NUM2’.

  1. if [ NUM1 -ne NUM2 ]; then

The condition is true if ‘NUM1’ is not same or equal to that of ‘NUM2’.

  1. if [ NUM1 -gt NUM2 ]; then

The condition is true if ‘NUM1’ is a greater value than that of ‘NUM2’.

  1. if [ NUM1 -ge NUM2 ]; then

The condition is true if ‘NUM1’ is greater than that of ‘NUM2’ or are equal to one another.

  1. if [ NUM1 -lt NUM2 ]; then

The condition is true if value in ‘NUM1’ is less than that of value in ‘NUM2’.

  1. if [ NUM1 -le NUM2 ]; then

The condition is true if the value in ‘NUM1’ is less than that of ‘NUM2’ or if both the numbers are equal to one another.

Final Words

These are just a few of the commonly preferred Bash If statements that are popularly used for usual Bash scripting. There are more to it, following to which you will also be using If-else Bash conditions for putting up more efficient and complex statements for desired output. So, make sure that you use them and utilize them to master the fundamentals of using if statements in bash scripting.

Are you aspiring to learn Bash scripting to create shell scripts to automate tasks on a UNIX system? If yes, enroll in the Bash Scripting Fundamentals training course and learn the fundamentals of Bash scripting at Whizlabs.