Basic Bash Programming Examples

25 practical examples of Bash scripting with code and output. Learn Bash programming fundamentals through these hands-on examples.

Example 1: Hello World
#!/bin/bash
echo "Hello, World!"
Hello, World!
The most basic Bash script that prints "Hello, World!" to the terminal.
Example 2: Variables
#!/bin/bash
name="John"
age=25
echo "My name is $name and I am $age years old."
My name is John and I am 25 years old.
Demonstrates how to declare and use variables in Bash.
Example 3: Command Substitution
#!/bin/bash
current_date=$(date)
echo "Current date and time: $current_date"
Current date and time: Wed Aug 25 14:32:15 UTC 2021
Shows how to capture the output of a command and store it in a variable.
Example 4: User Input
#!/bin/bash
echo "What's your name?"
read name
echo "Hello, $name!"
What's your name?
Alice
Hello, Alice!
Reads input from the user and uses it in the script.
Example 5: If Statement
#!/bin/bash
number=10
if [ $number -eq 10 ]; then
  echo "The number is 10"
fi
The number is 10
Basic conditional statement that checks if a variable equals 10.
Example 6: If-Else Statement
#!/bin/bash
number=15
if [ $number -lt 10 ]; then
  echo "Number is less than 10"
else
  echo "Number is greater than or equal to 10"
fi
Number is greater than or equal to 10
Demonstrates if-else conditional logic in Bash.
Example 7: For Loop
#!/bin/bash
for i in 1 2 3 4 5; do
  echo "Number: $i"
done
Number: 1
Number: 2
Number: 3
Number: 4
Number: 5
Basic for loop that iterates through a list of numbers.
Example 8: While Loop
#!/bin/bash
count=1
while [ $count -le 5 ]; do
  echo "Count: $count"
  count=$((count + 1))
done
Count: 1
Count: 2
Count: 3
Count: 4
Count: 5
A while loop that runs as long as the condition is true.
Example 9: Functions
#!/bin/bash
greet() {
  echo "Hello, $1!"
}

greet "Alice"
greet "Bob"
Hello, Alice!
Hello, Bob!
Defines and calls a simple function that takes a parameter.
Example 10: Arithmetic Operations
#!/bin/bash
a=10
b=3

echo "Addition: $((a + b))"
echo "Subtraction: $((a - b))"
echo "Multiplication: $((a * b))"
echo "Division: $((a / b))"
echo "Modulus: $((a % b))"
Addition: 13
Subtraction: 7
Multiplication: 30
Division: 3
Modulus: 1
Performs basic arithmetic operations using Bash.
Example 11: File Existence Check
#!/bin/bash
file="example.txt"
if [ -f "$file" ]; then
  echo "$file exists."
else
  echo "$file does not exist."
fi
example.txt does not exist.
Checks if a file exists using the -f flag.
Example 12: String Comparison
#!/bin/bash
str1="Hello"
str2="World"

if [ "$str1" = "$str2" ]; then
  echo "Strings are equal"
else
  echo "Strings are not equal"
fi
Strings are not equal
Compares two strings for equality.
Example 13: Case Statement
#!/bin/bash
fruit="apple"

case $fruit in
  "apple") echo "It's an apple.";;
  "banana") echo "It's a banana.";;
  *) echo "It's something else.";;
esac
It's an apple.
Uses a case statement to match against different patterns.
Example 14: Command Line Arguments
#!/bin/bash
echo "Script name: $0"
echo "First argument: $1"
echo "Second argument: $2"
echo "Total arguments: $#"
Script name: ./script.sh
First argument: arg1
Second argument: arg2
Total arguments: 2
Accesses command line arguments passed to the script.
Example 15: Arrays
#!/bin/bash
fruits=("Apple" "Banana" "Orange")

echo "First fruit: ${fruits[0]}"
echo "All fruits: ${fruits[@]}"
echo "Number of fruits: ${#fruits[@]}"
First fruit: Apple
All fruits: Apple Banana Orange
Number of fruits: 3
Demonstrates how to create and use arrays in Bash.
Example 16: Reading a File Line by Line
#!/bin/bash
while IFS= read -r line; do
  echo "Line: $line"
done < "example.txt"
Line: This is line 1
Line: This is line 2
Line: This is line 3
Reads a file line by line and processes each line.
Example 17: Sleep Command
#!/bin/bash
echo "Starting..."
sleep 2
echo "After 2 seconds"
Starting...
[2 second pause]
After 2 seconds
Pauses the script execution for a specified number of seconds.
Example 18: Exit Codes
#!/bin/bash
ls /nonexistentdirectory
echo "Exit code: $?"
ls: cannot access '/nonexistentdirectory': No such file or directory
Exit code: 2
Checks the exit code of the previous command.
Example 19: String Operations
#!/bin/bash
str="Hello World"

echo "Length: ${#str}"
echo "Uppercase: ${str^^}"
echo "Lowercase: ${str,,}"
echo "Substring: ${str:6:5}"
Length: 11
Uppercase: HELLO WORLD
Lowercase: hello world
Substring: World
Demonstrates various string manipulation operations.
Example 20: Debugging with set -x
#!/bin/bash
set -x

a=5
b=3
result=$((a + b))
echo "Result: $result"

set +x
echo "Debugging turned off"
+ a=5
+ b=3
+ result=8
+ echo 'Result: 8'
Result: 8
+ set +x
Debugging turned off
Shows how to enable and disable debugging in Bash scripts.
Example 21: Using bc for Floating Point
#!/bin/bash
a=10
b=3

echo "Scale=2; $a / $b" | bc
3.33
Uses the bc calculator for floating-point arithmetic.
Example 22: Here Document
#!/bin/bash
cat << EOF
This is a multiline
string using a here document
It can contain variables like $(date)
EOF
This is a multiline
string using a here document
It can contain variables like Wed Aug 25 14:32:15 UTC 2021
Uses a here document to output multiline text.
Example 23: Redirecting Output
#!/bin/bash
echo "This goes to stdout"
echo "This goes to stderr" >&2
echo "This goes to a file" > output.txt
This goes to stdout
This goes to stderr
Demonstrates output redirection to different destinations.
Example 24: Using grep in Script
#!/bin/bash
echo -e "apple\nbanana\norange" > fruits.txt
grep "na" fruits.txt
banana
orange
Uses grep to search for patterns in a file.
Example 25: Simple Calculator
#!/bin/bash
add() {
  echo $(($1 + $2))
}

result=$(add 5 3)
echo "5 + 3 = $result"
5 + 3 = 8
A simple calculator function that adds two numbers.