# Bash essentials
December 23, 2025 Linux Shell Fundamentals
Bash is the de-factor command line shell for linux. While not the most intuitive, it can be extremely terse yet powetful. Anyone who uses linux should have knowledge of at least the basics of bash.
#!/bin/bash
echo "Hello world"
$ bash ./script.sh
# make the script executable
$ chmod +x ./script.sh
# run directly
$ ./script.sh
Variables
NAME="Moeen"
MESSAGE="Hello world"
echo $MESSAGE
echo "My name ${NAME}"
- It is a convention that all variables should be uppercase
- There shouldn’t be a space before or after the equals (i.e.
=) sign when declaring variables - Variables can be interpolated in strings. This only works in double-quote strings
Arrays
declare -a packages=(
"libx11-dev"
"libfontconfig1-dev"
"libxft-dev"
)
# loop through the array
for pkg in ${packages[@]}; do
echo $pkg
done
# concatenate array values
packages_str=""
for pkg in ${packages[@]}; do
packages_str="${packages_str}${pkg} "
done
echo $packages_str
User Input
Interactive input
read -p "Enter name: " NAME
echo "Your name is ${NAME}"
Command-line Arguments
# print all args including script name
echo $0 $1 $2
# print all args excluding script name
echo $@
Capture output of command
NAME=$(whoami)
Control Flow
NAME="User"
if [[ $NAME == "User" ]]; then
echo "You are ${NAME}"
elif [[ $NAME == "Admin" ]]; then
echo "You are z -> ${NAME}"
else
echo "You are unknown"
fi
Value Comparison
A=30
B=40
# -eq (==) values are equal
# -ne (!=) values are not equal
# -gt first value greater than second value
# -ge first value greater or equal to second value
# -lt first value less than second value
# -le first value less than or equal to second value
if [[ $A -eq $B ]]; then
echo "values are equal"
fi
File Checks
FILENAME="example.txt"
# -e check if exists
# -d check if directory
# -f check if file
# -r check if file is readable
# -w check if file is writable
# -x check if file is executable
if [[ -e $FILENAME ]]; then
echo "file exists"
fi
Execute Commands
update_cmd="sudo apt-get update && sudo apt-get upgrade -y"
eval $update_cmd
Function
function greet() {
name=$1
echo "Hello, $name!"
}
greet 'admin'
function combine() {
input=$@
result=""
for entry in $input; do
result+="$entry, "
done
echo $result
}
declare -a roles=('admin' 'customer' 'another')
combine "${roles[@]}"