Keeping scripts safe stops bad things from happening to your computer or data. It helps protect your work and privacy.
When writing scripts that handle passwords or secret keys
When sharing scripts with others or running scripts from the internet
When automating tasks that change important files or system settings
When running scripts on a server or multi-user computer
When your script uses input from users or other programs
Syntax
Bash Scripting
# Example of safe script practices# 1. Use strict modeset -euo pipefail
# 2. Check inputsif [[ $# -eq 0 ]]; then
echo "Error: Missing argument" >&2
exit 1
fi
# 3. Avoid running commands as root unless needed# 4. Use full paths for commands
/usr/bin/grep "pattern" file.txt
# 5. Do not store passwords in plain text
set -euo pipefail helps catch errors early and avoid unexpected behavior.
Always check inputs to avoid running dangerous commands by mistake.
Examples
This is called 'strict mode' and makes your script safer by stopping on errors.
Bash Scripting
set -euo pipefail
# This stops the script if any command fails, if you use an unset variable, or if a pipeline fails.
This checks if no argument is provided and stops the script with an error message.
Bash Scripting
if [[ $# -eq 0 ]]; then
echo "Error: Missing argument" >&2
exit 1
fi
Using full paths avoids running the wrong program if someone changes your PATH.
Bash Scripting
/usr/bin/grep "pattern" file.txt
Storing passwords in scripts can expose them to others.
Bash Scripting
# Never store passwords like this:
PASSWORD="mypassword"# Instead, read from secure input or environment variables.
Sample Program
This script safely checks for a filename argument, verifies the file can be read, and searches for the word 'root' using the full path to grep.
Bash Scripting
#!/bin/bashset -euo pipefail
# Check if user provided a filenameif [[ $# -eq 0 ]]; then
echo "Usage: $0 filename" >&2
exit 1
fi
FILE="$1"# Check if file exists and is readableif [[ ! -r "$FILE" ]]; then
echo "Error: File '$FILE' does not exist or is not readable." >&2
exit 1
fi
# Use full path for grep
/usr/bin/grep "root""$FILE" || echo "No 'root' found in $FILE"
OutputSuccess
Important Notes
Always run scripts with the least privileges needed.
Never trust user input without checking it first.
Keep sensitive data out of scripts or use secure storage methods.
Summary
Use strict mode (set -euo pipefail) to catch errors early.
Check all inputs before using them in your script.
Use full paths for commands and avoid storing secrets in plain text.
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: