In bash conditional expression, we have many operators which used to compare variables or values and help to take decision on which further action is depend. So these conditional expression operators are really very important.
If we like to make our bash scripting skill effective, we should aware of these conditional expression operators. In this post we will try to cover many of these operators. These are broadly like below type.

1. Primary expression operators
2. Integers comparison operators
3. String comparison operators
4. Compound comparison operators
5. Checking exit status

1. Primary comparison operators


These expression operators are widely used for various Operating system file and directories checks. In Bash manual page these all things are well documented. Trying to mention some of them below

Operator Significance
-a FILE True if file exists.
-b FILE True if file exists and is a block special file.
-c FILE True if FILE exists and is a character-special file.
-d FILE True if file exists and is a directory.
-e FILE True if file exists.
-f FILE True if file exists and is a regular file.
-g FILE True if file exists and is set-group-id.
-h FILE True if file exists and is a symbolic link.
-k FILE True if file exists and its “sticky” bit is set.
-p FILE True if file exists and is a named pipe (FIFO).
-r FILE True if file exists and is readable.
-s FILE True if file exists and has a size greater than zero.
-t FILE True if file descriptor fd is open and refers to a terminal.
-u FILE True if file exists and its set-user-id bit is set.
-w FILE True if file exists and is writable.
-x FILE True if file exists and is executable.
-G FILE True if file exists and is owned by the effective group id.
-N FILE True if file exists and has been modified since it was last read.
-O FILE True if file exists and is owned by the effective user id.
-S FILE True if file exists and is a socket.
-S FILE True if file exists and is a socket.
file1 -ef file2 True if file1 and file2 refer to the same device and inode numbers.
file1 -nt file2 True if file1 is newer (according to modification date) than file2, or if file1 exists and file2 does not.
file1 -ot file2 True if file1 is older than file2, or if file2 exists and file1 does not.
-o optname True if the shell option optname is enabled. See the list of options under the description of the -o option to the set builtin below.
-v varname True if the shell variable varname is set (has been assigned a value).
-R varname True if the shell variable varname is set and is a name reference.
arg1 OP arg2 OP is one of -eq, -ne, -lt, -le, -gt, or -ge. These arithmetic binary operators return true if arg1 is equal to, not equal to, less than, less than or equal to, greater than, or greater than or equal to arg2, respectively. Arg1 and arg2 may be positive or negative integers.

How to Use


These condition expressions are used many times in bash scripting for file-system or operating system checks. Let’s see some of these examples in some small bash scripts.

#!/bin/bash
read -p "Please mentioned executable file name: " executable
if [ -x $executable ]; then 
echo "$executable is present with execute permission"
else
echo "$executable file not exists or is not executable."
fi
=====================================
        OutPut
#./bash_primary_condition_expression
Please mentioned executable file name: /bin/bash
/bin/bash is present with execute permission
#./bash_primary_condition_expression
Please mentioned executable file name: /etc/passwd
/etc/passwd file not exists or is not executable.

With above example we can easily understand how we can use these conditional expression operators. These are used under [ ] with if condition for some decision making tasks. This can also be used with very simple one liner bash commands.

#[[ (-f /bin/bash ) && ( -x /bin/bash) ]] && echo "/bin/bash is executable file" ||echo "/bin/bash is not present or executable"
/bin/bash is executable file
#[[ (-f /bin/fstab ) && ( -x /bin/fstab) ]] && echo "/bin/fstab is executable file" || echo "/bin/fstab is not present or executable"
/bin/fstab is not present or executable

2. Integers comparison operators
In bash, there is some inbuilt comparison operators used for integers comparison. Please note integer and string use different set of comparison operators.
 

Operator Significance
-eq is equal to.
-ne not equal to.
-ne not equal to.
-gt is greater than.
-ge is greater or equal to.
-lt is less than.
-le is less than or equal to.
-ne not equal to.

How to Use


These operators can also use in same as above does. we can use them in many ways, let’s explore few of them here with below examples.

#!/bin/bash
read -p "Type first ineteger: " number1
read -p "Type second ineteger: " number2
[ $number1 -le $number2 ] && echo "$number1 less than or equal to $number2" || echo "$number1 is not less than or equal to $number2"
=====================================
        OutPut
#./ineteger_opertors
Type first ineteger: 12
Type second ineteger: 23
12 less than or equal to 23
#./ineteger_opertors
Type first ineteger: 23
Type second ineteger: 12
23 is not less than or equal to 12

Use of these operators is based on their exit status while running or comparing them. we can used them as we did above or in if condition, in if condition we can decide task on basis of their exit status.
exit status 0 means success or True and exit status other than 0 means fail or false. Let’s try to explain same with very basic example, that could use in various ways in bash scripting

#[ 2 -le 3 ]; echo $?
0
#[ 4 -le 3 ]; echo $?
1

 

3. String comparison operators


There are some inbuilt string comparison operator, which were used to do string operation in bash script.

Operator Significance
-z string True if the length of string is zero.
-n string True if the length of string is non-zero.
string1 == string2 True if the strings are equal. = should be used with the test command for POSIX conformance. When used with the [[ command, this performs pattern matching as described above (Compound Commands).
string1 != string2 True if the strings are not equal.
string1 < string2 True if string1 sorts before string2 lexicographically.
string1 > string2 True if string1 sorts after string2 lexicographically.

Let try to understand their use with some easy examples in normal bash script or one liner commands.

#[ -n "`grep sukrant /etc/passwd`" ] && echo "Locally manage user" || echo "User is not managed locally"
Locally manage user

#[ -n "`grep iwejfwkf /etc/passwd`" ] && echo "Locally manage user" || echo "User is not managed locally"
User is not managed locally

#if [ -n "`grep sukrant /etc/passwd`" ]; then echo "Locally manage user" ;else  echo "User is not managed locally"; fi 
Locally manage user

Rest will cover in next part