<aside> ⚠️ The space after the opening & before the closing paranthesis / bracket is very important. Not adding a white space will cause an error.
</aside>
String Comparisons
[[ $s1 = $s2 ]] or [[ $s1 == $s2 ]] → Compare if two strings are equal.[[ $s1 != $s2 ]] → Compare if two strings are not equal.[[ $s1 < $s2 ]]< → Is $s1 less than $s2, in ASCII alphabetical order.> → Is $s1 greater than $s2, in ASCII alphabetical order.[ $s1 ] → True if $s1 is not empty.[ -n $s1 ] → True if length of $s1 is greater than 0.[ -z $s1 ] → True if length of $s1 is equal to 0.Number Comparisons
[ n1 -eq n2 ] → True if $n1 equal to$n2.[ n1 -ne n2 ] → True if $n1 is not equal to $n2 .[ n1 -ge n2 ] → True if $n1 is greater than or equal to $n2.[ n1 -gt n2 ] → True if $n1 is greater than $n2.[ n1 -le n2 ] → True if $n1 is less than or equal to $n2.[ n1 -lt n2 ] → True if $n1 is less than $n2.File Test Operator:
[ -e $file_name] → Test if a file named file_name exists.[ -f $file_name] → Test if a file named file_name exists and is a regular file or not.[ -b $file_name] → Test if a file named file_name exists and is a block special file or not.[ -c $file_name] → Test if a file named file_name exists and is a character special file or not.[ -d $dir] → Test if a directory named dir exists.[ -s $file_name] → Test if a file named file_name is empty.[ -r $file_name] → Test if a file named file_name has read permission.[ -w $file_name] → Test if a file named file_name has write permission.[ -x $file_name] → Test if a file named file_name has execute permission.Logical Operator
AND operation
[ expression1 ] && [ expression2 ][ expression1 -a expression2 ][[ expression1 && expression2 ]]OR operation
[ expression1 ] || [ expression2 ][ expresson1 -o expression2 ][[ expression1 || expression2 ]]Integer Arithmetic Operator
<aside> 📌 The below mentioned operators only works on integer type numbers.
</aside>
$(( num1 + num2 )) or $(expr $num1 + $num2 ) → Addition
$(( num1 - num2 )) or $(expr $num1 - $num2 )→ Subtraction
$(( num1 * num2 )) or $(expr $num1 \* $num2 )→ Multiplication
<aside>
⚠️ In the second method i.e., expr, do not miss the escape character \.
</aside>
$(( num1 / num2 )) or $(expr $num1 / $num2 )→ Division
$(( num1 % num2 )) or $(expr $num1 % $num2 )→ Modulus
Floating Point Arithmetic Operation
To perform floating point arithmetic operations we need to use a tool called bc. The way to use the bc command is to pass the expression as an input to the command.
"$num1+$num2" | bc → Floating point Addition"$num1-$num2" | bc → Floating point Subtraction"$num1*$num2 "| bc → Floating point Multiplication"scale=x;$num1/$num2" | bc → Floating point Division
By default the bc command will not give any decimal output i.e., 20.5/5 = 4. To be able to get the decimal part of the output we have to set the scale of the output i.e., scale=2;20.5/5 = 4.10."$num1%$num2" | bc → Floating point ModulusWe use the -l option with the bc command to enable the math library.
"$num1^$num2" | bc -l → Exponentiation"scale=x;sqrt($num1)" | bc -l → Square root<aside>
💡 The bc command has a lot more features. Check out its man page to know more.
</aside>
Increment & Decrement Operators
$(( n++ )) →Post Increment$(( n-- )) →Post Decrement$(( ++n )) →Pre Increment$(( --n )) →Pre Decrement