Bash — Shell Scripting
Argument parsing
#!/bin/bash
while [[ $# -gt 0 ]]; do
case $1 in
-n|--name) name="$2"; shift 2 ;;
-a|--age) age="$2"; shift 2 ;;
*) echo "Unknown: $1"; shift ;;
esac
done
Error handling
#!/bin/bash
set -euo pipefail
trap 'echo "Error on line $LINENO"' ERR
Logging
#!/bin/bash
LOG_FILE="app.log"
log() {
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" | tee -a "$LOG_FILE"
}
log "Starting script"
log "Processing files"
Main function
#!/bin/bash
main() {
echo "Starting..."
# Do work
echo "Done!"
}
main "$@"
Mini Practice
Write Bash code that:
- Parses command-line arguments
- Sets up error handling with trap
- Creates a logging function
- Uses a main function pattern
Up Next
In the next lesson, you'll learn about Cron Jobs — scheduled tasks.
Related Topics
Frequently Asked Questions about Shell Scripting
What is Shell Scripting in Bash?
Shell Scripting is a fundamental concept in Bash. This lesson explains it step by step with clear examples, making it easy for beginners to understand.
How do I learn Shell Scripting?
Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Shell Scripting.
Why is Shell Scripting important in Bash?
Shell Scripting is essential for Bash development. Understanding this concept will help you write better code and solve real-world problems more effectively.