Bash & Linux·Bash from first command to automation

Shell scripting

Write safe scripts with strict mode, variables, conditions and functions.

Beginner 15 min read Lesson 7 of 300 Challenge included
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.

backup.shTerminal
#!/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

Add strict mode

Easy

Add strict mode and a shebang to a new Bash script.

BASH · starter
#!/bin/bash
Check 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 in
Finished reading?

Lock in what you just learned.

Mark this lesson complete to save your place and move one step closer to the certificate.

Next: Environment and PATH
Lesson 7 of 300 · 0% of course