Bash Conditional operators

The most important thing is to keep track of the indents before and after the square brackets.

#!/bin/bash # We accept the first parameter from the terminal mode=$1 echo "start" if [[ $mode = 1 ]]; then echo "First" elif [ $mode = 2 ]; then echo "Second" else echo "Try using different parameter" fi

Comparison operators

-n - string is not zero

-z is a null string, that is, it has zero length

Create a non-empty string and apply -t and -z to it

foo="bar";
[ -n "$foo" ] && echo "foo is not null"

foo is not null

[ -z "$foo" ] && echo "foo is null"

Now let's create an empty string

foo="";
[ -n "$foo" ] && echo "foo is not null"

[ -z "$foo" ] && echo "foo is null"

foo is null

↓ Bash articles ↓
Bash
Bash Scripting
-eq -gt …: Comparison operators
while loop
date
Newline
Arithmetic operations
Pass a file or password over SFTP in a Bash script
Errors Bash

Article author: Andrei Olegovich