📖 Scenario: You are creating a simple Bash script to manage user backups on a shared server. To keep the server safe, you want to follow script security best practices.
🎯 Goal: Build a Bash script step-by-step that uses secure coding practices like setting safe permissions, avoiding running as root, and validating inputs.
📋 What You'll Learn
Create a variable with a backup directory path
Add a variable to hold the username to backup
Use a conditional to check if the user exists
Print a secure message confirming the backup
💡 Why This Matters
🌍 Real World
System administrators often write scripts to automate backups and user management. Following security best practices prevents accidental damage or security breaches.
💼 Career
Knowing how to write secure Bash scripts is essential for roles like DevOps engineers, system administrators, and security analysts.
Progress0 / 4 steps
1
Create the backup directory variable
Create a variable called backup_dir and set it to /home/backups.
Bash Scripting
Hint
Use double quotes around the path string for safety.
2
Add the username variable
Create a variable called username and set it to guest.
Bash Scripting
Hint
Use double quotes around the username string.
3
Check if the user exists
Use an if statement with id -u $username to check if the user exists. If the user exists, print User exists, else print User does not exist.
Bash Scripting
Hint
Use id -u $username and redirect errors to /dev/null to check user existence.
4
Print a secure backup confirmation
Print a message using echo that says Backup for user guest will be stored in /home/backups using the variables username and backup_dir.
Bash Scripting
Hint
Use echo with variables inside double quotes for safe output.
Practice
(1/5)
1. Which of the following is a key reason to use set -euo pipefail at the start of a bash script?
easy
A. It automatically logs all commands to a file for debugging.
B. It speeds up the script execution by running commands in parallel.
C. It stops the script immediately if any command fails or if an unset variable is used.
D. It disables all user inputs to prevent accidental changes.
Solution
Step 1: Understand what set -euo pipefail does
This command combination makes the script exit if any command fails (-e), if an unset variable is used (-u), and if any part of a pipeline fails (pipefail).
Step 2: Identify the effect on script safety
By stopping early on errors or unset variables, it prevents the script from continuing in an unexpected or unsafe state.
Final Answer:
It stops the script immediately if any command fails or if an unset variable is used. -> Option C
Quick Check:
Strict mode = stops on errors [OK]
Hint: Remember: strict mode stops on errors and unset variables [OK]
Common Mistakes:
Thinking it speeds up script execution
Assuming it logs commands automatically
Believing it disables user input
2. Which of the following is the correct way to check if a variable user_input is set and not empty in bash?
easy
A. if [ $user_input = "" ]; then echo "Empty"; fi
B. if [ -z "$user_input" ]; then echo "Empty"; fi
C. if [ -n $user_input ]; then echo "Empty"; fi
D. if [ "$user_input" == null ]; then echo "Empty"; fi
Solution
Step 1: Understand the test for empty string
The test [ -z "$user_input" ] returns true if the variable is unset or empty, which is the correct way to check emptiness safely.
Step 2: Check other options for correctness
if [ "$user_input" == null ]; then echo "Empty"; fi compares to 'null' string which is incorrect. if [ -n $user_input ]; then echo "Empty"; fi lacks quotes and reverses logic. if [ $user_input = "" ]; then echo "Empty"; fi lacks quotes and can cause errors if variable is unset.
Final Answer:
if [ -z "$user_input" ]; then echo "Empty"; fi -> Option B
Quick Check:
Use -z with quotes to check empty safely [OK]
Hint: Use quotes and -z to check empty variables safely [OK]
Common Mistakes:
Not quoting variables causing errors
Comparing to 'null' string instead of empty
Using wrong test operators
3. What will be the output of this script snippet if PATH is not set and set -u is enabled?
#!/bin/bash
set -u
echo "Path is: $PATH"
medium
A. No output, script runs silently
B. Path is:
C. Path is: /usr/bin:/bin
D. bash: PATH: unbound variable
Solution
Step 1: Understand set -u effect
The set -u option causes the script to exit with an error if an unset variable is referenced.
Step 2: Analyze the script behavior
Since PATH is not set, referencing $PATH triggers an unbound variable error and the script stops with a message.
Final Answer:
bash: PATH: unbound variable -> Option D
Quick Check:
Unset variable with set -u causes error [OK]
Hint: set -u errors on unset variables immediately [OK]
Common Mistakes:
Assuming empty string output instead of error
Thinking script runs silently
Assuming default PATH value is used
4. Identify the security issue in this script snippet: