What Is Bash Scripting?
Bash (Bourne Again SHell) is the default shell on most Linux distributions, and it's also a full scripting language. A bash script is simply a text file containing a series of commands that the shell executes in order. Scripts let you automate repetitive tasks, schedule maintenance jobs, and build complex workflows from simple building blocks.
Your First Bash Script
Create a new file called hello.sh:
#!/bin/bash
# This is a comment
echo "Hello, Linux!"
The first line (#!/bin/bash) is called a shebang. It tells the operating system which interpreter to use. Make the script executable and run it:
chmod +x hello.sh
./hello.sh
Variables
Variables store data you can reuse throughout your script. There are no spaces around the = sign:
#!/bin/bash
NAME="Alice"
CURRENT_DATE=$(date +%Y-%m-%d)
echo "Hello, $NAME!"
echo "Today is $CURRENT_DATE"
Use $(command) to capture the output of a command into a variable. Always quote variables in double quotes ("$VAR") to handle spaces safely.
User Input
Read input from the user with the read command:
#!/bin/bash
echo -n "Enter your username: "
read USERNAME
echo "Welcome, $USERNAME!"
Conditional Statements
Use if, elif, and else to make decisions:
#!/bin/bash
FILE="/etc/passwd"
if [ -f "$FILE" ]; then
echo "$FILE exists."
elif [ -d "$FILE" ]; then
echo "$FILE is a directory."
else
echo "$FILE does not exist."
fi
Common test operators include:
-f file— True if file exists and is a regular file.-d dir— True if directory exists.-z "$VAR"— True if string is empty.-n "$VAR"— True if string is not empty.$A -eq $B— True if integers are equal.$A -gt $B— True if A is greater than B.
Loops
For Loop
#!/bin/bash
for i in 1 2 3 4 5; do
echo "Number: $i"
done
# Loop over files in a directory
for file in /var/log/*.log; do
echo "Processing: $file"
done
While Loop
#!/bin/bash
COUNT=0
while [ $COUNT -lt 5 ]; do
echo "Count: $COUNT"
COUNT=$((COUNT + 1))
done
Functions
Functions help you organize reusable blocks of code:
#!/bin/bash
greet_user() {
local USER=$1
echo "Hello, $USER! Welcome to $(hostname)."
}
greet_user "Alice"
greet_user "Bob"
Arguments are accessed with $1, $2, etc. Use local to scope variables inside functions.
A Practical Example: Backup Script
#!/bin/bash
# Simple backup script
SOURCE_DIR="$HOME/documents"
BACKUP_DIR="$HOME/backups"
TIMESTAMP=$(date +%Y%m%d_%H%M%S)
BACKUP_FILE="$BACKUP_DIR/docs_backup_$TIMESTAMP.tar.gz"
# Create backup directory if it doesn't exist
mkdir -p "$BACKUP_DIR"
# Create compressed archive
tar -czf "$BACKUP_FILE" "$SOURCE_DIR"
if [ $? -eq 0 ]; then
echo "Backup successful: $BACKUP_FILE"
else
echo "Backup failed!" >&2
exit 1
fi
$? holds the exit code of the last command (0 = success, non-zero = error). The >&2 redirects the error message to stderr.
Tips for Better Scripts
- Add
set -eat the top to exit immediately on any error. - Add
set -uto treat unset variables as errors. - Always quote variables: use
"$VAR"not$VAR. - Test scripts with
bash -n script.shto check syntax without executing. - Use
shellcheck— a static analysis tool that catches common bash mistakes.
Bash scripting is a skill that compounds over time. Start with small scripts for tasks you do daily, and gradually take on more complex challenges. The Linux shell is endlessly powerful when you learn to speak its language.