Each shell supports various basic operations. The most popular shell is the Bourne Again Shell, or bash, which comes as a default with most Linux distributions.
There are five basic operations that one must know to use the bash shell:
Bash supports the following arithmetic operators:
Operator | Description | Example |
Addition | Adds the operands on either side | expr $a + $b |
Subtraction | Subtracts the right hand operand from the left hand | expr $a - $b |
Multiplication | Multiplies two operands | expr $a * $b |
Division | Divides the right hand operand with the left hand operand and returns the quotient | expr $a / $b |
Modulus | Divides the right hand operand with the left hand operand and returns the remainder | expr $a % $b |
Increment | Unary operator to increment an operand by one | expr $((++a)) |
Decrement | Unary operator to decrement an operand by one | expr $((--a)) |
a=4b=5echo "a + b = $((a + b))"echo "a - b = $((a - b))"echo "a * b = $((a * b))"echo "a / b = $((a / b))"echo "a % b = $((a % b))"echo "++a = $((++a))"echo "--b = $((--b))"
Bash supports relational operators that work on variables with numeric values or strings that are numeric. Relational operators do not work on strings if their values are not numeric.
Relational operators either give true
or false
depending on the relation.
Operator | Description | Example |
== | Compares two operands and returns true if they are equal; otherwise, it returns false | [$a == $b] |
!= | Compares two operands and returns true if they are not equal; otherwise, it returns false | [$a != $b] |
< | Less than operator; returns true if the left hand operand is less than the right hand operand | [$a < $b] |
<= | Less than or equal to operator; returns true if the left hand operand is less than or equal to the right hand operand | [$a <= $b] |
> | Greater than operator; returns true if the left hand operand is greater than the right hand operand | [$a > $b] |
>= | Greater than or equal to operator; returns true if the left hand operand is greater than or equal to the right hand operand | [$a >= $b] |
a=100b=10if(( $a == $b ))thenecho "a and b are equal"fiif(( $a != $b ))thenecho "a and b are not equal"fiif(( $a > $b ))thenecho "a is greater than b"elseecho "a is not greater than b"fiif(( $a >= $b ))thenecho "a is greater or equal to than b"elseecho "a is not greater or equal to than b"fi
The following boolean operators are supported by bash:
Operator | Description | Example |
! | The logical negation operator | [ ! false ] |
-o ( || ) | The logical OR operator | [ $a < 20 || $b > 30 ] |
-a ( && ) | The logical AND operator | [ $a < 20 & $b > 30 ] |
echo "LOGICAL_AND = $((1 && 0))"echo "LOGICAL_OR = $((0 || 1))"echo "LOGICAL_Neg = $((!0))"
Bitwise operators are used to perform bitwise operations on bit fields.
Operators | Description | Example |
& | Performs the binary AND operation bit by bit on the arguments | echo "$((0x4 & 0x1))" |
| | Performs the binary OR operation bit by bit on the arguments | echo "$((0x4 | 0x1))" |
^ | Performs the binary XOR operation bit by bit on the arguments | echo "$((0x4 ^ 0x1))" |
~ | Performs the binary NOT operation bit by bit on the arguments | echo "$((~0x4))" |
<< | Shifts the bit field to the left by the number of times specified by the right hand operand | echo "$((0x4 << 1))" |
>> | Shifts the bit field to the right by the number of times specified by the right hand operand | echo "$((0x4 >> 1))" |
echo "0x4 & 0x0 = $((0x4 & 0x0))"echo "0x4 | 0x1 = $((0x4 | 0x1))"echo "0x1 ^ 0x1 = $((0x1 ^ 0x1))"echo "0x1 << 4 = $((0x1 << 4))"echo "0x4 >> 2 = $((0x4 >> 2))"
Bash provides operators to test for various properties of files; these operators are known as file test operators.
Operator | Description | Example |
-b | Checks whether a file is a block special file or not | [ -b $FileName] |
-c | Checks whether a file is character special or not | [ -c $FileName] |
-d | Checks if a given directory exists or not | [ -d $FileName] |
-e | Checks whether a given file exists or not | [ -e $FileName] |
-r | Checks if the given file has read access or not | [ -r $FileName] |
-w | Checks if the given file has write access or not | [ -w $FileName] |
-x | Checks if the given file has execute access or not | [ -x $FileName] |
-s | Checks the size of the given file | [ -s $FileName] |
read -p "Enter filename " FileNameif [ -e $FileName ]thenecho "$FileName exists"if [ -r $FileName ]thenecho "$FileName has read access"elseecho "$FileName does not have read access!"fiif [ -w $FileName ]thenecho "$FileName has write access"elseecho "$FileName does not have write access!"fiif [ -x $FileName ]thenecho "$FileName has execute access"elseecho "$FileName does not have execute access!"fiif [ -s $FileName ]thenecho "$FileName size is non-zero"elseecho "$FileName is empty"fielseecho "$FileName not found!"fi
Enter the input below
Free Resources