Ultimate Bash Scripting Handbook
Master the essential skill for Unix and Linux productivity. From first-time shebang execution and string quotes to positional parameters, reusable functions, file operations, and pipeline redirection.
- Shebang syntax (#!) and multi-platform interpreter selection
- Single vs. double quotations and escape string evaluation
- System-defined environment vs. custom variable scoping
- Conditionals, loops (for, while, until), and command-line arguments
- File redirection (stdin, stdout, stderr) and Unix pipelines
#!/bin/bash
# Author: Sagar Biswas
# Description: Automated Safe Backup
set -euo pipefail
DEST_DIR="/backup/daily"
TIMESTAMP=$(date +%Y%m%d_%H%M%S)
log_message() {
echo "[$(date +%T)] $1"
}
if [[ ! -d "$DEST_DIR" ]]; then
mkdir -p "$DEST_DIR"
log_message "Created directory $DEST_DIR"
fi
tar -czf "$DEST_DIR/backup_${TIMESTAMP}.tar.gz" \
/var/log /etc/scripts 2>/dev/null
log_message "Backup completed successfully."
exit 0