Why Learn Bash Scripting?
If you find yourself typing the same sequence of commands day after day — backing up files, renaming batches of photos, checking disk usage, updating software — you're doing work a script could handle in seconds. Bash (Bourne Again Shell) is the default shell on most Linux systems and macOS, making it one of the most universally useful automation tools you can learn.
You don't need to be a developer to write useful Bash scripts. Even simple two-line scripts can save meaningful time.
Your First Bash Script
A Bash script is just a plain text file containing shell commands. Create a new file:
nano hello.sh
Add the following content:
#!/bin/bash
echo "Hello, World!"
echo "Today is: $(date)"
The first line — #!/bin/bash — is called a shebang. It tells the system which interpreter to use. Always include it.
Save the file, then make it executable:
chmod +x hello.sh
Run it:
./hello.sh
Variables
Variables store values you can reuse throughout the script:
#!/bin/bash
NAME="TechPath"
echo "Welcome to $NAME Guide"
Note: no spaces around the = sign. Use $VARIABLE_NAME to reference a variable.
Reading User Input
#!/bin/bash
echo "Enter your name:"
read USERNAME
echo "Hello, $USERNAME!"
Conditional Logic (if/else)
Scripts become powerful when they make decisions:
#!/bin/bash
DISK_USAGE=$(df / | tail -1 | awk '{print $5}' | tr -d '%')
if [ "$DISK_USAGE" -gt 80 ]; then
echo "Warning: Disk usage is at ${DISK_USAGE}%"
else
echo "Disk usage is fine: ${DISK_USAGE}%"
fi
This script checks root disk usage and warns if it's above 80%. You could run this automatically with a cron job (more on that below).
Loops
Use loops to repeat actions. A common example — rename all .txt files to add a date prefix:
#!/bin/bash
DATE=$(date +%Y-%m-%d)
for FILE in *.txt; do
mv "$FILE" "${DATE}_${FILE}"
done
echo "Renamed all .txt files with today's date."
A Practical Example: Automated Backup Script
#!/bin/bash
SOURCE_DIR="$HOME/Documents"
BACKUP_DIR="$HOME/Backups"
DATE=$(date +%Y-%m-%d)
BACKUP_FILE="$BACKUP_DIR/documents_$DATE.tar.gz"
mkdir -p "$BACKUP_DIR"
tar -czf "$BACKUP_FILE" "$SOURCE_DIR"
echo "Backup created: $BACKUP_FILE"
This script compresses your Documents folder into a timestamped archive. Run it daily and you have a rolling backup history.
Scheduling Scripts with Cron
To run your backup script every day at 2 AM, add a cron job:
crontab -e
Add this line:
0 2 * * * /home/username/backup.sh >> /home/username/backup.log 2>&1
Cron syntax follows the format: minute hour day month weekday command. The >> redirects output to a log file so you can review results.
Tips for Writing Better Scripts
- Add comments with
#to explain what sections do — your future self will thank you - Quote variables (
"$VAR") to handle filenames with spaces safely - Test incrementally — run sections in isolation before chaining them together
- Use
set -eat the top to make the script exit immediately on any error - Use
echoliberally for progress messages while testing
Where to Go Next
Once you're comfortable with variables, conditionals, and loops, explore functions (reusable code blocks), argument parsing ($1, $2 for script arguments), and more advanced text processing with awk and sed. Bash scripting has a learning curve, but the payoff — hours of tedious work automated away — makes it one of the most practical skills in any tech toolkit.