Conditional expressions are evaluated at script execution time, based on results, It executes speci fic command blocks.

thes

There are different types of conitional expressions in Bash

  • String Comparision Operators
  • Numric Comparision Operator
  • File Operators
  • Logical Operators

File Operators

Bash provides logical operators on FIle and directories to test condtional expressions. It allows you to check different operations such as existence, and permissions, size. It is used conditional expressions in conditional statements such as if else and case.

Syntax:

if [[ conditiona_expressions]]; then
# code to handle
fi

conditiona_expressions contains options, and filepath, that always returns true or false.

following are options provided

OperatorDescription
-e fileReturns true if given file exists, file can be normal file or directory
-f fileReturns true if given file exists and a file(not directory)
-d fileReturns true if file is an directory
-r fileReturns true if file exists and has readable permission
-w fileReturns true if file exists and has writable permission
-x fileReturns true if file exists and has executable permission
-s fileReturns true if file exists and size is not empty
-G fileReturns true if file exists and isowned by a Group ID that matches
-O fileReturns true if file exists and owned by a user ID that matches
-N fileReturns true if file exists and modified by last read date
-L fileReturns true if file exists and and is an symbolic Link
file1 -ot file2Returns true if file1 is older than file2 or file2 exists, file1 does not exist
file1 -ne file2Returns true if file1 is newer than file2,file1 exists, file2 does not exists
file1 -ef file2Returns true if file1 and file2 pointed to same device and inode