Conditional Logic
Conditional operations have the objective to check if an expression is true or false.
To check something like this, you might want to use the command test
test 10 -gt 20
test 'hello' = 'hellow'
test -d /etc/
Or within a bash script.
[ 10 -gt 20 ]
[ 'hello' = 'hellow' ]
[ -d /etc/ ]
The syntax of conditional logic in bash differs from other programming languages.
Syntax | Description |
---|---|
-ne |
!= Not equal to |
-gt |
> Greater than |
-ge |
>= Greater than or equal to |
-lt |
< Less than |
-le |
>= Less than or equal to |
The IF conditional
Execute one or more instructions only if a condition is met
Syntax
if CONDITION; then
commands
fi
Example:
if [ $v1 -gt $v2 ]; then
echo "The variable v1 is bigger than the variable v2"
fi