Why should you avoid running scripts as the root user unless absolutely necessary?
Running scripts as root can cause serious damage if the script has errors or is malicious. It can change or delete important system files. Always use the least privilege needed to reduce risk.
Click to reveal answer
beginner
What is the purpose of using 'set -e' at the start of a bash script?
'set -e' makes the script stop immediately if any command fails. This helps prevent the script from continuing in a broken state, which can cause security or data problems.
Click to reveal answer
intermediate
How can you protect sensitive data like passwords in your scripts?
Avoid hardcoding passwords in scripts. Use environment variables, encrypted files, or secure vaults. Also, restrict script file permissions so only trusted users can read them.
Click to reveal answer
intermediate
Why should you validate all user inputs in scripts?
Validating inputs prevents injection attacks and unexpected behavior. It ensures the script only processes safe and expected data, reducing security risks.
Click to reveal answer
advanced
What is the risk of using 'eval' in bash scripts, and how can you avoid it?
'eval' executes strings as commands, which can be dangerous if the string contains malicious code. Avoid using 'eval' or sanitize inputs carefully before using it.
Click to reveal answer
What does 'chmod 700 script.sh' do to the script file?
AAllows everyone to read and execute the script
BAllows only the owner to read, write, and execute the script
CMakes the script read-only for everyone
DRemoves all permissions from the script
✗ Incorrect
'chmod 700' sets permissions so only the file owner can read, write, and execute the script. This protects the script from unauthorized access.
Which of these is a good practice to avoid exposing passwords in scripts?
AStore passwords in environment variables
BHardcode passwords directly in the script
CPrint passwords to the console
DShare passwords in comments
✗ Incorrect
Storing passwords in environment variables keeps them out of the script code and reduces the risk of accidental exposure.
What is the main reason to validate user input in scripts?
ATo increase script size
BTo make the script run faster
CTo prevent injection attacks and errors
DTo allow any input without restrictions
✗ Incorrect
Validating input helps prevent malicious data from causing harm or unexpected behavior in the script.
What does the 'set -u' option do in a bash script?
ADisables error checking
BIgnores unset variables
CPrints all commands before executing
DTreats unset variables as an error and exits
✗ Incorrect
'set -u' helps catch errors by stopping the script if it tries to use a variable that has not been set.
Why is it risky to use 'eval' with user input in bash scripts?
AIt can execute malicious commands
BIt makes the script shorter
CIt slows down the script
DIt prevents errors
✗ Incorrect
'eval' runs the input as code, so if the input is malicious, it can harm the system or leak data.
Explain three best practices to keep bash scripts secure.
Think about permissions, input safety, and secrets.
You got /3 concepts.
Describe why 'set -e' and 'set -u' are useful in secure bash scripting.
Consider how these options help prevent unexpected script behavior.
You got /3 concepts.
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: