Linux Shell Scripting
Automate repetitive tasks and build powerful scripts with Bash. Shell scripting is the duct tape of system administration — it is how you turn a sequence of commands you type repeatedly into something the machine does for you. Every DevOps pipeline, every deployment script, every monitoring check started as a shell script.Basic Script Structure
Every Bash script starts with a shebang line that tells the system which interpreter to use.Variables
Conditionals
Bash conditionals test exit codes. In Linux, an exit code of 0 means success (true), and anything else means failure (false). This is the opposite of most programming languages.File and String Tests
Modern Test Syntax
Loops
Functions
Functions let you organize code into reusable blocks. They are essential once your script grows beyond 20-30 lines.Practical Script: Deployment Example
Here is a realistic script that ties together variables, conditionals, functions, and error handling. This is the kind of script you would actually use in production — notice how every step validates its preconditions, logs what it does, and has a path for failure.Cron Jobs
Cron is the Linux scheduler. It runs scripts at specified times, unattended. Think of it as setting an alarm for your server.Key Takeaways
- Start every script with
#!/bin/bashandset -euo pipefail - Always quote variables (
"$VAR") to handle spaces and special characters safely - Use
localin functions to avoid polluting the global scope - Test files with
-f,-d,-r,-sbefore operating on them - Use
[[ ]](double brackets) instead of[ ]for safer, more powerful tests - Cron runs in a minimal environment — use full paths and redirect output
- Write logs with timestamps in production scripts so you can trace what happened
Congratulations — you have completed the Linux Crash Course. Next: Docker Crash Course →