Shell scripting
Write safe scripts with strict mode, variables, conditions and functions.
What you will be able to do
- Start with #!/usr/bin/env bash and set -euo pipefail.
- Quote every variable: "$name".
- Use [[ ]] for conditions.
- Use functions and local variables.
In plain English
A script is a program. Start with a shebang and set -euo pipefail so failures stop the script and unset variables are errors. Quote variables, use functions for repeated logic and return meaningful exit codes.
#!/usr/bin/env bash
set -euo pipefail
target="${1:?usage: backup.sh <dir>}"
stamp="$(date +%Y%m%d-%H%M%S)"
tar -czf "backup-${stamp}.tar.gz" "$target"
echo "wrote backup-${stamp}.tar.gz"Your turn
EasyAdd strict mode
Add strict mode and a shebang to a new Bash script.
BASH · starter
#!/bin/bashCheck your understanding before moving on.
Knowledge checkQuestion 1
What does set -e do?
Select an answer to continue
Study tools
Make it yours.
Keep a private note for this lesson, then turn the key ideas into flashcards you can revisit.
Sign in to save notes and flashcards.Both are private to your account and stay with your progress.
Sign inFinished reading?
Lock in what you just learned.
0%
Mark this lesson complete to save your place and move one step closer to the certificate.