Comprehensive Bash Programming Guide

O

Ohidur Rahman Bappy

MAR 22, 2025

Comprehensive Bash Programming Guide

Introduction

Bash is a powerful scripting language that provides tools for automating tasks. This guide offers a comprehensive cheatsheet to help you understand the fundamentals of Bash programming.

Basics

Echo Command

echo 'Hello World!'

Variables

Uppercase by convention, using letters, numbers, and underscores.

NAME="Bob"
echo "My name is $NAME"
echo "My name is ${NAME}"

User Input

read -p "Enter your name: " NAME
echo "Hello $NAME, nice to meet you!"

Conditional Statements

Simple If Statement

if [ "$NAME" == "Bappy" ]
then
  echo "Your name is Bappy"
fi

If-Else

if [ "$NAME" == "Bappy" ]
then
  echo "Your name is Bappy"
else 
  echo "Your name is NOT Bappy"
fi

Else-If (elif)

if [ "$NAME" == "Bappy" ]
then
  echo "Your name is Bappy"
elif [ "$NAME" == "Jack" ]
then  
  echo "Your name is Jack"
else 
  echo "Your name is NOT Bappy or Jack"
fi

Comparisons

NUM1=31
NUM2=5
if [ "$NUM1" -gt "$NUM2" ]
then
  echo "$NUM1 is greater than $NUM2"
else
  echo "$NUM1 is less than $NUM2"
fi

Operators:

val1 -eq val2   Returns true if the values are equal
val1 -ne val2   Returns true if the values are not equal
val1 -gt val2   Returns true if val1 is greater than val2
val1 -ge val2   Returns true if val1 is greater than or equal to val2
val1 -lt val2   Returns true if val1 is less than val2
val1 -le val2   Returns true if val1 is less than or equal to val2

File Conditions

FILE="test.txt"
if [ -e "$FILE" ]
then
  echo "$FILE exists"
else
  echo "$FILE does NOT exist"
fi

File condition checks:

-d file   True if the file is a directory
-e file   True if the file exists
-f file   True if the provided string is a file
-g file   True if the group id is set on a file
-r file   True if the file is readable
-s file   True if the file has a non-zero size
-u        True if the user id is set on a file
-w        True if the file is writable
-x        True if the file is an executable

Case Statement

read -p "Are you 21 or over? Y/N " ANSWER
case "$ANSWER" in 
  [yY] | [yY][eE][sS])
    echo "You can have a beer :)"
    ;;
  [nN] | [nN][oO])
    echo "Sorry, no drinking"
    ;;
  *)
    echo "Please enter y/yes or n/no"
    ;;
esac

Loops

Simple For Loop

NAMES="Bappy Kevin Alice Mark"
for NAME in $NAMES
do
echo "Hello $NAME"
done

For Loop to Rename Files

FILES=$(ls *.txt)
NEW="new"
for FILE in $FILES
do
  echo "Renaming $FILE to new-$FILE"
  mv $FILE $NEW-$FILE
done

While Loop - Read through a File Line by Line

LINE=1
while read -r CURRENT_LINE
do
  echo "$LINE: $CURRENT_LINE"
  ((LINE++))
done < "./new-1.txt"

Functions

Basic Function

function sayHello() {
  echo "Hello World"
}
sayHello

Function with Parameters

function greet() {
  echo "Hello, I am $1 and I am $2"
}
greet "Bappy" "36"

Directory and File Operations

Create Folder and Write to File

mkdir hello
touch "hello/world.txt"
echo "Hello World" >> "hello/world.txt"
echo "Created hello/world.txt"

With this guide, you should have a good foundational understanding of Bash scripting. Continue practicing and exploring more advanced concepts to level up your scripting skills.