Shell Scripting Basics
6 min read Quiz at the end
Shell scripts automate tasks. Start with #!/bin/bash and add set -euo pipefail to exit on errors. Make executable with chmod +x script.sh. Use variables, if conditions, and loops just like a programming language.
Writing Shell Scripts #!/bin/bash
# backup.sh
SOURCE="/var/www"
DEST="/backup/$(date +%Y%m%d)"
mkdir -p "$DEST"
cp -r "$SOURCE" "$DEST"
echo "Backup done: $DEST"chmod +x backup.sh
./backup.sh
Topic Quiz · 5 questions
Test your understanding before moving on
1. What is the shebang line?
A. #!/bin/bash at top — tells OS which interpreter to use
B. A comment style
C. A variable declaration
D. An error handler
💡 #!/bin/bash (or #!/usr/bin/env bash) tells the OS which interpreter to use.
2. How do you make a script executable?
A. chmod 644
B. chmod +r
C. chmod +x
D. chmod 400
💡 chmod +x script.sh adds execute permission so you can run it.
3. How do you run a script?
A. script.sh
B. run script.sh
C. ./script.sh
D. execute script.sh
💡 ./script.sh runs the script from the current directory.
4. What does $1 mean in a script?
A. First character
B. PID
C. First argument passed to script
D. Script name
💡 $1 is the first positional argument: ./script.sh arg1 → $1 = "arg1".
5. $@ in a script means:
A. First argument
B. Script name
C. All arguments
D. Number of arguments
💡 $@ expands to all positional arguments as separate words.
Submit answers