Shell Scripts
Automate repetitive tasks by writing shell scripts with variables, loops, and conditionals.
What you'll learn
- How to write and execute a shell script
- Variables, conditionals, and loops in bash
- Functions and arguments for reusable scripts
- Making scripts executable with chmod
Concept
What Is a Shell Script?
A shell script is a plain text file containing a sequence of shell commands that the shell executes in order. Instead of typing the same commands over and over, you write them once in a file and run the file whenever you need. Shell scripts are the simplest form of automation and are used everywhere in DevOps — for deployments, backups, environment setup, and more.
Every shell script starts with a shebang line — #!/bin/bash — that tells the operating system which interpreter to use. Without the shebang, the system may use the wrong shell and your script will behave unexpectedly.
Variables and Conditionals
You can store values in variables without any type declaration: NAME="value". To read a variable, prefix it with a dollar sign: $NAME. Conditionals use if, then, else, fi syntax. You can test file existence with -f, directory existence with -d, and compare strings or numbers. These constructs let your script make decisions based on the environment.
Loops and Functions
Loops let you repeat actions. A for loop iterates over a list of items; a while loop runs as long as a condition is true. Functions group related commands and can accept arguments accessed via $1, $2, etc. Together, loops and functions let you write scripts that are both powerful and maintainable.
Making Scripts Executable
After writing a script, you must give it execute permission with chmod +x script.sh. Then you can run it as ./script.sh. Without the execute permission, the OS will refuse to run it. You can also run a script directly with bash script.sh, which does not require the execute permission but does require the shebang to be correct for portability.
Real-World Usage
Shell scripts are used extensively in DevOps for deployment automation, environment provisioning, backup routines, and log rotation. They are the glue between different tools and services. However, for complex logic or large scripts, consider a language like Python or Go — they offer better error handling, testing, and maintainability. Use shell scripts for simple automation and glue code, and reach for a full language when the script grows beyond a few dozen lines or needs error handling beyond simple exit codes.
Example
#!/bin/bash
# A deployment helper script
APP_NAME="myapp"
ENVIRONMENT=${1:-staging} # first argument, default "staging"
echo "Deploying $APP_NAME to $ENVIRONMENT..."
if [ ! -f "Dockerfile" ]; then
echo "Error: Dockerfile not found!"
exit 1
fi
for service in api web worker; do
echo " Building $service..."
docker build -t "$APP_NAME-$service:latest" "./$service"
done
echo "Deployment to $ENVIRONMENT complete."
This script demonstrates variables, default arguments, conditionals, and loops. It checks for a Dockerfile, then builds three service images in a loop.
Try it
- Cron Parser
Parse cron expressions used in shell automation.
- Crontab Generator
Generate crontab entries for scheduled scripts.
- Regex Tester
Test regex patterns for grep and sed in scripts.
Common mistakes
The mistake
Forgetting the shebang line
The fix
Always start scripts with #!/bin/bash (or #!/usr/bin/env bash for portability). Without it, the script may run in a different shell with different syntax.
The mistake
Not quoting variables
The fix
Always quote variables: "$VAR" not $VAR. Unquoted variables break on spaces and empty values, causing subtle bugs.
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Build real projects
Apply what you learned by building guided projects with starter code and solutions.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.