The moment you type the same sequence of commands more than twice, it belongs in a script instead. A shell script is just the commands you'd type by hand, saved to a file, with variables and conditionals to make it handle more than one exact situation.
3 min read
At its simplest, a shell script is a text file containing exactly the commands you'd otherwise type one at a time, run all together as one unit:
#!/bin/bash
echo "Starting backup..."
tar -czf backup.tar.gz /home/alice/documents
echo "Backup complete."The first line, #!/bin/bash, is called a shebang — it tells the system
which interpreter should execute this file when it's run directly, rather
than requiring you to type bash script.sh explicitly every time:
$ chmod +x backup.sh # make it executable (this domain's permissions lesson, directly)
$ ./backup.sh # run it directly, using the shebang to find bash automatically#!/bin/bash
BACKUP_DIR="/home/alice/documents"
DEST="backup-$(date +%Y-%m-%d).tar.gz"
echo "Backing up $BACKUP_DIR to $DEST"
tar -czf "$DEST" "$BACKUP_DIR"$(date +%Y-%m-%d) is command substitution — running a command and
inserting its output directly into the surrounding string, here generating a
filename that includes today's actual date rather than a fixed, unchanging
one.
#!/bin/bash
if [ -f "config.txt" ]; then
echo "Config found, proceeding."
else
echo "No config file found — aborting."
exit 1
fi[ -f "config.txt" ] tests whether a file exists; exit 1 stops the script
with a non-zero exit code, the standard convention meaning "this failed" —
directly relevant to CI/CD pipelines (covered in the Cloud, Infrastructure &
DevOps domain), which check a script's exit code to decide whether a step
succeeded or the whole pipeline should stop.
#!/bin/bash
for file in *.txt; do
echo "Processing $file"
gzip "$file"
doneThis compresses every .txt file in the current directory, one at a time —
exactly the kind of repeated, mechanical task that's tedious and error-prone
done by hand, and reliable and instant once captured in a script.
#!/bin/bash
# Usage: ./greet.sh Alice
echo "Hello, $1!"$1 refers to the first argument passed when running the script
(./greet.sh Alice prints "Hello, Alice!"); $2, $3 and so on continue for
additional arguments, and $@ refers to all of them together.
The practical trigger this lesson opened with: once you've typed the same sequence of commands more than twice, write it as a script. For anything more complex — real error handling, structured data, something that needs to be tested — a real programming language (Python, per this app's own curriculum) is usually the better tool; shell scripting shines specifically for gluing together existing command-line tools, not for building complex application logic from scratch.
Check your understanding
A quick comprehension check — not tracked, not graded, just for you.
1. What does the shebang line (#!/bin/bash) at the top of a script file do?
2. What is command substitution, as used in `DEST="backup-$(date +%Y-%m-%d).tar.gz"`?
3. According to this lesson, what's a reasonable trigger for deciding to write a shell script?
Linux & the Command Line