A shell script is a text file containing a sequence of shell commands, marked executable and run as a program. Shell scripts are how Unix systems have historically automated tasks: installing software, managing backups, deploying services, gluing programs together. The shell is itself a programming language—with variables, loops, conditionals, and functions—that happens to have command execution as its fundamental operation.
A minimal script:
#!/usr/bin/env bash
set -euo pipefail
name="${1:-world}"
echo "Hello, $name!"
The #!/usr/bin/env bash shebang tells the kernel which interpreter to run the script with. set -euo pipefail turns on strict mode (exit on error, undefined variables are errors, pipeline failures propagate), which is strongly recommended for any non-trivial script.
For anything more than ~100 lines, consider whether Python, Go, or another proper language would serve better. Shell is uniquely suited to chaining external commands but has fragile quoting, limited data structures, and subtle evaluation rules. The linter shellcheck catches many common mistakes and is invaluable for maintaining production scripts.
Discussed in:
- Chapter 14: Shell Scripting — Your First Script
Also defined in: Textbook of Linux