If Else Statements
Basics
if <condition>; then
# Commands if condition is true
fi
if <condition>; then
# Commands if condition is true
else
# Commands if condition is false
fi
if [[ <condition> ]]; then
# Commands if condition is true
else
# Commands if condition is false
fi
[[ <condition> ]] && # Command is true
[[ <condition> ]] && # Command if true || # Command if falseNOTE: type
man testto see some possible checks that can be used for if-else conditions.
Checks
Check if command exists or installed
command -v <command-to-check>β Method 1hash <command-to-check>β Method 2type <command-to-check>β Method 3which <command-to-check>β Not recommended
Samples
command -v tmux >/dev/null 2>&1
# or
if ! command -v tmux &> /dev/null
then
echo "Tmux could not be found"
exit
fiBreak-like Statement for If Statements
Using : built-in shell command that does nothing.
if [[ <condition> ]]; then
echo "Some commands"
elif [[ <condition> ]]; then
:
else
echo "Another commands"
fiRef: