Bash Scripting
Published on June 6, 2025•by Tech Notes Hub•Linux•Documentation
Last updated: June 6, 2025
#Bash Scripting
Bash (Bourne Again SHell) is a command language interpreter that is widely used on various operating systems, and is the default shell on most Linux distributions.
#Introduction to Bash Scripting
Bash scripts are text files containing a series of commands that are executed by the Bash shell. They allow you to automate repetitive tasks, combine complex commands, and create custom utilities.
#Basic Syntax
#Creating a Bash Script
- Create a file with a
.sh
extension - Add the shebang line at the top:
#!/bin/bash
- Make the script executable:
chmod +x script.sh
- Run the script:
./script.sh
#Hello World Example
#!/bin/bash
# This is a comment
echo "Hello, World!"
#Variables
#Variable Declaration and Usage
#!/bin/bash
# Variable declaration
name="John"
age=30
# Using variables
echo "Name: $name"
echo "Age: $age"
# Command substitution
current_date=$(date)
echo "Current date: $current_date"
# Arithmetic operations
result=$((10 + 5))
echo "10 + 5 = $result"
#Special Variables
Variable | Description |
---|---|
$0 | The name of the script |
$1 to $9 | The first 9 arguments passed to the script |
$# | The number of arguments passed to the script |
$@ | All arguments passed to the script |
$? | The exit status of the last command |
$$ | The process ID of the current script |
$USER | The username of the user running the script |
$HOSTNAME | The hostname of the machine |
$RANDOM | A random number |
$HOME | The home directory of the user |
#Control Structures
#Conditional Statements
#If-Else Statement
#!/bin/bash
age=25
if [ $age -lt 18 ]; then
echo "You are a minor."
elif [ $age -ge 18 ] && [ $age -lt 65 ]; then
echo "You are an adult."
else
echo "You are a senior."
fi
#Case Statement
#!/bin/bash
fruit="apple"
case $fruit in
"apple")
echo "This is an apple."
;;
"banana")
echo "This is a banana."
;;
"orange")
echo "This is an orange."
;;
*)
echo "Unknown fruit."
;;
esac
#Loops
#For Loop
#!/bin/bash
# Simple for loop
for i in 1 2 3 4 5; do
echo "Number: $i"
done
# For loop with range
for i in {1..5}; do
echo "Number: $i"
done
# For loop with step
for i in {1..10..2}; do
echo "Odd number: $i"
done
# For loop with command output
for file in $(ls); do
echo "File: $file"
done
#While Loop
#!/bin/bash
count=1
while [ $count -le 5 ]; do
echo "Count: $count"
((count++))
done
#Until Loop
#!/bin/bash
count=1
until [ $count -gt 5 ]; do
echo "Count: $count"
((count++))
done
#Functions
#Function Definition and Usage
#!/bin/bash
# Function definition
greet() {
echo "Hello, $1!"
}
# Function with return value
add() {
local result=$(($1 + $2))
echo $result
}
# Function calls
greet "John"
sum=$(add 5 3)
echo "5 + 3 = $sum"
#Input and Output
#Reading User Input
#!/bin/bash
# Read a single value
echo "Enter your name:"
read name
echo "Hello, $name!"
# Read multiple values
echo "Enter your first and last name:"
read first_name last_name
echo "Hello, $first_name $last_name!"
# Read with prompt
read -p "Enter your age: " age
echo "You are $age years old."
# Read password (hidden input)
read -sp "Enter your password: " password
echo -e "\nPassword received."
#File Input/Output
#!/bin/bash
# Writing to a file
echo "Hello, World!" > output.txt
echo "This is a new line." >> output.txt
# Reading from a file
while IFS= read -r line; do
echo "Line: $line"
done < input.txt
# Process each line of a file
cat input.txt | while read line; do
echo "Processing: $line"
done
#Arrays
#Array Operations
#!/bin/bash
# Declare an array
fruits=("apple" "banana" "orange" "grape")
# Access array elements
echo "First fruit: ${fruits[0]}"
echo "All fruits: ${fruits[@]}"
echo "Number of fruits: ${#fruits[@]}"
# Iterate through array
for fruit in "${fruits[@]}"; do
echo "Fruit: $fruit"
done
# Add element to array
fruits+=("kiwi")
# Remove element from array
unset fruits[1]
#String Manipulation
#String Operations
#!/bin/bash
# String length
str="Hello, World!"
echo "Length: ${#str}"
# Substring
echo "Substring: ${str:7:5}"
# String replacement
echo "Replace: ${str/World/Bash}"
# Convert to uppercase/lowercase
echo "Uppercase: ${str^^}"
echo "Lowercase: ${str,,}"
#Error Handling
#Basic Error Handling
#!/bin/bash
# Exit on error
set -e
# Custom error handling
handle_error() {
echo "Error occurred at line $1"
exit 1
}
# Trap errors
trap 'handle_error $LINENO' ERR
# Check command success
if ! command -v git &> /dev/null; then
echo "Git is not installed."
exit 1
fi
#Best Practices
- Use Shebang: Always include
#!/bin/bash
at the top of your scripts. - Comments: Add comments to explain complex logic.
- Error Handling: Implement proper error handling.
- Indentation: Use consistent indentation for readability.
- Naming Conventions: Use descriptive names for variables and functions.
- Quoting Variables: Always quote variables to handle spaces and special characters.
- Exit Codes: Return appropriate exit codes.
- Modularity: Break complex scripts into functions.
- Debugging: Use
set -x
for debugging. - Testing: Test your scripts with different inputs.
#References
- GNU Bash Manual
- Bash Guide for Beginners
- Advanced Bash-Scripting Guide
- ShellCheck - A shell script analysis tool
Code Snippets
Tags:
'learningtechnologyprogramming'
Source:
Sourced from: docs/linux/bash-scripting.md