Table of Contents
- Bash Variables: Storing and Reusing Values
- Command Substitution: Capturing Output Dynamically
- Pipes and Redirection: Controlling Input/Output
- Loops: Automating Repetitive Tasks
- Conditionals: Making Decisions in Scripts
- Functions: Reusing Code with Custom Commands
- Find: Searching for Files Efficiently
- Xargs: Passing Output as Arguments
- Sed: Stream Editing for Text Manipulation
- Awk: Advanced Text Processing and Reporting
- Cron: Scheduling Recurring Tasks
- Traps: Cleaning Up After Scripts
- Conclusion
- References
1. Bash Variables: Storing and Reusing Values
Variables let you store data (text, numbers, file paths, etc.) and reuse it throughout a script. They’re foundational for making scripts dynamic and readable.
How to Use Variables
- Declare/Assign: Use
variable_name=value(no spaces around=). - Access: Prefix with
$(e.g.,$variable_name). - Quoting: Use double quotes (
" ") to preserve spaces/whitespace; single quotes (' ') treat text literally.
Examples
# Assign a string
greeting="Hello, Bash!"
echo $greeting # Output: Hello, Bash!
# Store a file path
log_dir="/var/log"
echo "Logs are stored in: $log_dir" # Output: Logs are stored in: /var/log
# User input (read from stdin)
read -p "Enter your name: " username
echo "Welcome, $username!" # Output: Welcome, [input]!
Environment vs. Local Variables
- Environment Variables: Global variables available to all processes (e.g.,
$HOME,$PATH,$USER). Set withexport variable=value. - Local Variables: Only available in the current script/terminal. Defined without
export.
2. Command Substitution: Capturing Output Dynamically
Command substitution lets you capture the output of a command and use it as a value in a variable or another command. This is critical for dynamic automation (e.g., using the current date in a filename).
Syntax
- Use
$(command)(preferred) or backticks`command`.
Examples
# Get current date (e.g., 2024-05-20)
current_date=$(date +%Y-%m-%d)
echo "Today is: $current_date" # Output: Today is: 2024-05-20
# Count files in the current directory
file_count=$(ls | wc -l)
echo "There are $file_count files here." # Output: There are X files here.
# Store the output of a script in a variable
script_output=$(./backup_script.sh)
echo "Backup result: $script_output"
3. Pipes and Redirection: Controlling Input/Output
Bash lets you redirect command input/output to files or chain commands together with pipes (|), enabling powerful data processing workflows.
Redirection Operators
| Operator | Purpose | Example |
|---|---|---|
> | Overwrite output to a file | ls > file_list.txt |
>> | Append output to a file | echo "New line" >> notes.txt |
2> | Redirect errors to a file | rm non_existent_file 2> error.log |
&> | Redirect both output and errors to a file | command &> output_and_errors.log |
< | Read input from a file | grep "error" < app.log |
Pipes (|)
Chain commands: The output of the first command becomes the input of the second.
Examples
# List all .txt files and count them (ls → wc)
ls *.txt | wc -l
# Find lines containing "ERROR" in app.log and save to errors.txt (grep → redirect)
grep "ERROR" app.log > errors.txt
# Run a command, suppress all output (send to /dev/null)
command &> /dev/null
4. Loops: Automating Repetitive Tasks
Loops let you repeat commands multiple times (e.g., process files in a directory, wait for a condition).
For Loops
Iterate over a list (files, numbers, strings).
Syntax
for item in list; do
# commands using $item
done
Examples
# Process all .log files in /var/log
for logfile in /var/log/*.log; do
echo "Processing: $logfile"
gzip $logfile # Compress the log file
done
# Loop from 1 to 5
for i in {1..5}; do
echo "Count: $i"
done
While Loops
Repeat commands as long as a condition is true.
Syntax
while condition; do
# commands
done
Examples
# Read lines from a file until end-of-file (EOF)
while read line; do
echo "Line: $line"
done < input.txt
# Wait until a service is online (check every 5 seconds)
while ! ping -c 1 google.com &> /dev/null; do
echo "Waiting for internet..."
sleep 5
done
echo "Internet connected!"
5. Conditionals: Making Decisions in Scripts
Conditionals (if-else, case) let scripts make decisions based on criteria like file existence, command success, or user input.
If-Else Statements
Check a condition and run commands accordingly.
Syntax
if condition; then
# commands if true
elif another_condition; then
# commands if first condition false, second true
else
# commands if all conditions false
fi
Common Conditions
| Condition | Checks if… | Example |
|---|---|---|
-f file | file exists and is a regular file | if [ -f "data.txt" ]; then ... |
-d dir | dir exists and is a directory | if [ -d "/tmp" ]; then ... |
-z "string" | string is empty | if [ -z "$username" ]; then ... |
$a -eq $b | Numeric equality (a == b) | if [ $count -eq 10 ]; then ... |
"$a" == "$b" | String equality | if [ "$status" == "success" ]; then ... |
Example: Check File Existence
file="important_data.txt"
if [ -f "$file" ]; then
echo "$file exists. Backing up..."
cp "$file" "$file.bak"
elif [ -d "$file" ]; then
echo "$file is a directory, not a file!"
else
echo "$file does not exist."
fi
Case Statements
Simplify multiple if-elif checks for a single variable.
Syntax
case $variable in
pattern1)
commands
;;
pattern2)
commands
;;
*) # Default case (matches anything)
commands
;;
esac
Example: Menu Script
read -p "Choose an option (1-3): " choice
case $choice in
1)
echo "Option 1 selected: Backup files"
# Run backup commands
;;
2)
echo "Option 2 selected: Cleanup logs"
# Run cleanup commands
;;
3)
echo "Option 3 selected: Exit"
exit 0
;;
*)
echo "Invalid option!"
;;
esac
6. Functions: Reusing Code with Custom Commands
Functions let you group commands into reusable blocks, making scripts modular and easier to maintain.
Syntax
function_name() {
# commands
# Access parameters with $1, $2, etc. (like script arguments)
}
Example: Backup Function
# Define a function to backup a directory
backup_dir() {
local dir="$1" # First parameter: directory to backup
local dest="$2" # Second parameter: destination path
if [ -d "$dir" ]; then
echo "Backing up $dir to $dest..."
rsync -av "$dir" "$dest" # Use rsync for efficient backup
else
echo "Error: $dir is not a valid directory!"
return 1 # Return error code
fi
}
# Use the function
backup_dir "/home/user/documents" "/mnt/backup/docs"
7. Find: Searching for Files Efficiently
The find command locates files/directories by criteria like name, size, or modification time. It’s indispensable for bulk file operations.
Common find Options
| Option | Purpose | Example |
|---|---|---|
-name "pattern" | Match filenames (supports wildcards) | find /home -name "*.txt" |
-type f/d | Search for files (f) or directories (d) | find /var -type d -name "log*" |
-mtime -7 | Modified in the last 7 days (- = newer, + = older) | find /tmp -mtime +30 (older than 30 days) |
-size +10M | Larger than 10MB (+ = larger, - = smaller) | find / -size +1G (larger than 1GB) |
-exec cmd {} \; | Run cmd on found files ({} = placeholder for filename) | find . -name "*.log" -exec gzip {} \; |
Example: Delete Old Logs
# Find all .log files in /var/log older than 30 days and delete them
find /var/log -name "*.log" -type f -mtime +30 -exec rm {} \;
8. Xargs: Passing Output as Arguments
xargs converts input (e.g., from a pipe) into command arguments, solving issues with long lists of files (e.g., rm $(find ...) may fail for many files).
Syntax
command | xargs [options] target_command
Examples
# Find all .txt files and concatenate them into one file (find → xargs → cat)
find . -name "*.txt" | xargs cat > combined.txt
# Safely delete files with spaces in names (use -print0 with find and -0 with xargs)
find . -name "file with spaces.txt" -print0 | xargs -0 rm
9. Sed: Stream Editor for Text Manipulation
sed (stream editor) modifies text in a pipeline or file without opening an editor. It’s ideal for search-and-replace, deletion, or insertion.
Common sed Commands
| Command | Purpose | Example |
|---|---|---|
s/old/new/ | Replace first occurrence of “old” with “new” | sed 's/error/ERROR/' app.log |
s/old/new/g | Replace all occurrences (global) | sed 's/foo/bar/g' document.txt |
d | Delete lines matching a pattern | sed '/^#/d' config.conf (delete comments) |
i\line | Insert “line” before matching lines | sed '/^start/i\=== BEGIN ===' file.txt |
Example: Replace Text in a File
# Replace "http" with "https" in all .html files (in-place edit with -i)
sed -i 's/http/https/g' *.html
10. Awk: Advanced Text Processing and Reporting
awk is a powerful language for parsing structured text (e.g., CSVs, logs) and generating reports. It splits input into fields (columns) and processes them with patterns/commands.
Key Features
- Fields:
$0(entire line),$1(1st column),$2(2nd column), etc. - Variables:
NR(line number),NF(number of fields in the line). - Conditions:
if,else, and pattern matching.
Examples
# Extract the 2nd column from a CSV file (comma-separated)
awk -F ',' '{print $2}' data.csv
# Sum the 3rd column of numbers in a file
awk '{sum += $3} END {print "Total:", sum}' sales_data.txt
# Print lines where the 1st column is "ERROR" (log processing)
awk '$1 == "ERROR" {print $0}' app.log
11. Cron: Scheduling Recurring Tasks
Cron is a system daemon for scheduling tasks to run at fixed intervals (e.g., daily backups, hourly log checks).
Cron Syntax
A crontab entry has 5 time fields + a command:
* * * * * command_to_run
- - - - -
| | | | |
| | | | +-- Day of week (0-6, 0=Sunday)
| | | +---- Month (1-12)
| | +------ Day of month (1-31)
| +-------- Hour (0-23)
+---------- Minute (0-59)
Common Special Characters
*: Every value (e.g.,*in the minute field = every minute).*/5: Every 5 units (e.g.,*/5in the hour field = every 5 hours).3,7: Specific values (e.g.,3,7in the day field = 3rd and 7th).
How to Use Cron
- Edit your crontab with
crontab -e. - Add a line for your task.
Example: Daily Backup at 2 AM
# Backup /home/user to /mnt/backup daily at 2:00 AM
0 2 * * * rsync -av /home/user /mnt/backup/daily_$(date +\%Y\%m\%d)
Note: Use \% for date formatting in cron (escape the %).
12. Traps: Handling Errors and Cleanup
Traps let you run commands when a script receives a signal (e.g., Ctrl+C or exit), ensuring cleanup (e.g., deleting temporary files) even if the script fails.
Syntax
trap 'command' SIGNAL
Common signals:
EXIT: Triggered when the script exits (success or failure).SIGINT(2): Triggered byCtrl+C.
Example: Clean Up Temporary Files
# Create a temporary file
temp_file=$(mktemp)
# Trap EXIT to delete the temp file when the script exits
trap 'rm -f "$temp_file"; echo "Cleaned up temporary file."' EXIT
# Script logic...
echo "Working with $temp_file..."
# If the script is interrupted (Ctrl+C) or exits, the trap runs.
Conclusion
Mastering these Bash automation commands transforms you from a passive Linux user into an efficient power user. Whether you’re writing simple loops to process files or complex cron jobs to manage system tasks, these tools let you automate with precision and flexibility.
Start small: write a script to back up your documents, then expand to more advanced workflows with find, awk, or cron. The more you practice, the more you’ll uncover Bash’s potential to simplify your daily Linux life.