Cancel

Conditional Statements in Bash

The statement allows you to make decisions in your Bash scripts. It allows you to run a piece of code based upon conditions and make much more complex scripts, which may solve your larger tasks or problems.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# Simple `if-else` structure.
if [[ condition ]]; then
  # condition block
else
  # else block
fi

# check empty string! - option 1
# $1 can be Yafiz
# -z: variable is empty.
if [[ -z $1 ]]; then
  echo "ERROR: Name is required!"
else
  echo "My name is $1"
fi

# check empty string! - option 2
# $1 can be Yafiz
# -n: variable is not empty.
if [[ -n $1 ]]; then
  echo "My name is $1"
else
  echo "ERROR: Name is required!"
fi

# comparison operators
# -eq equals too.
# -ne not equals.
# -lt less than.
# -le less than or equal.
# -gt greater than.
# -ge greater than or equals.
#
# you can test all options one by one.
if [[ $1 -gt 100 ]]; then
  echo "$1 is larger than 100."
else
  echo "ERROR: $1 is smaller than 100."
fi

# (( NUM > NUM )) 2 round brackets can be used for numeric conditions
if (( $1 -gt 100 )); then
  echo "$1 is larger than 100."
else
  echo "ERROR: $1 is smaller than 100."
fi

# [[ STRING =~ STRING ]] tilde is used for Regular Expression
# $1 can be "https://google.com"
if [[ $1 =~ ^https ]]; then
  echo "Starts with HTTPs"
else
  echo "ERROR: No HTTPs Found"
fi

# File operators.
# -d directory exists.
# -f file exists.
# -r readable permission.
# -w writable permission.
# -x executable permission.
# $1 can be "$PWD/myfile.txt"
#
# you can test all options one by one.
if [[ -f $1 ]]; then
  echo "file is found."
else
  echo "ERROR: file is not found"
fi

# Relational operators
# == both values must be equals to each other.
# != both values must not be equals to each other.
#
# you can test all options one by one.
if [[ $1 == "Yafiz" ]]; then
  echo "Hello, Yafiz!"
else
  echo "ERROR: message can be here!"
fi

# Logical operators
# ! means the value is false or not valid
# && means both sides' conditions must be true, then execute the block.
# || means either side of the condition is true, then execute the block.
#
# you can test all options one-by-one.
if [[ $1 == "Yafiz" && $2 -eq 99 ]]; then
  echo "Hello, Yafiz!"
else
  echo "ERROR: message can be here!"
fi

# ternary operator or shorthand operator
[[ $1 == "Yafiz" ]] && echo "Hello, Yafiz Abraham!" || echo "$1 is not supported!"

# case statement
# better to use case statement instead of if-elif again n again.
# here only one time you need to call a variable.
# and check different values.
case $1 in
  fname)
    echo "Yafiz!"
    ;;
  lname)
    echo "Abraham"
    ;;
  *)
    echo "$1 is not supported"
    exit
esac