Bird
Raised Fist0
Bash Scriptingscripting~20 mins

Script security best practices in Bash Scripting - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Script Security Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
1:30remaining
Output of a script with safe variable usage
What is the output of this bash script when run?

#!/bin/bash
user_input="; rm -rf /"
echo "Hello, $user_input!"
Bash Scripting
#!/bin/bash
user_input="; rm -rf /"
echo "Hello, $user_input!"
AHello, ; rm -rf /!
BHello, !
CSyntax error
DCommand executed: rm -rf /
Attempts:
2 left
💡 Hint
Look at how the variable is used inside double quotes.
💻 Command Output
intermediate
1:30remaining
Effect of unquoted variables in bash scripts
What happens when this script runs?

#!/bin/bash
user_input="file.txt; rm -rf /"
cat $user_input
Bash Scripting
#!/bin/bash
user_input="file.txt; rm -rf /"
cat $user_input
AScript exits silently with no output
BSyntax error due to unquoted variable
COnly contents of file.txt printed safely
DContents of file.txt printed, then system tries to run rm -rf / command
Attempts:
2 left
💡 Hint
Think about how the shell interprets unquoted variables with special characters.
📝 Syntax
advanced
1:30remaining
Identify the secure way to read user input in bash
Which option securely reads user input into a variable without allowing command injection?
Aread user_input; echo $user_input
Bread user_input; eval "$user_input"
Cread user_input; echo "$user_input"
Dread user_input; bash -c "$user_input"
Attempts:
2 left
💡 Hint
Avoid running user input as code.
🔧 Debug
advanced
2:00remaining
Debugging a script vulnerable to command injection
This script is vulnerable to command injection. Which option fixes it?

#!/bin/bash
filename=$1
cat $filename
Bash Scripting
#!/bin/bash
filename=$1
cat $filename
Acat "$filename"
Bcat $filename | grep -v rm
Ccat ${filename}
Dcat `echo $filename`
Attempts:
2 left
💡 Hint
Quoting variables prevents word splitting and command injection.
🚀 Application
expert
2:30remaining
Secure script to safely handle filenames with spaces and special chars
You want to write a bash script that lists details of files given as arguments. Which script correctly handles filenames with spaces and special characters safely?
A
#!/bin/bash
for file in $*; do
  ls -l "$file"
done
B
#!/bin/bash
for file in "$@"; do
  ls -l "$file"
done
C
#!/bin/bash
for file in "$*"; do
  ls -l "$file"
done
D
#!/bin/bash
for file in $@; do
  ls -l $file
done
Attempts:
2 left
💡 Hint
Check how the script iterates over arguments and quotes variables.

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

  1. 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).
  2. 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.
  3. Final Answer:

    It stops the script immediately if any command fails or if an unset variable is used. -> Option C
  4. 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

  1. 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.
  2. 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.
  3. Final Answer:

    if [ -z "$user_input" ]; then echo "Empty"; fi -> Option B
  4. 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

  1. Step 1: Understand set -u effect

    The set -u option causes the script to exit with an error if an unset variable is referenced.
  2. 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.
  3. Final Answer:

    bash: PATH: unbound variable -> Option D
  4. 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:
#!/bin/bash
read -p "Enter filename: " file
cat $file
medium
A. Using read without validation allows command injection.
B. No issue, script is safe as is.
C. Missing quotes around $file causes syntax error.
D. Using cat is insecure; should use less instead.

Solution

  1. Step 1: Analyze input handling

    The script reads user input directly into file and uses it without validation or sanitization.
  2. Step 2: Understand risk of command injection

    If the user inputs something like file.txt; rm -rf /, the shell will execute the dangerous command, causing security risk.
  3. Final Answer:

    Using read without validation allows command injection. -> Option A
  4. Quick Check:

    Unvalidated input risks command injection [OK]
Hint: Always validate or sanitize user input before use [OK]
Common Mistakes:
  • Ignoring input validation
  • Thinking missing quotes cause syntax error here
  • Believing command choice alone fixes injection
5. You want to securely run a script that uses a password stored in a file. Which practice improves security best?
hard
A. Read the password from a file with restricted permissions and never echo it.
B. Store the password in plain text in the script for easy access.
C. Pass the password as a command line argument to the script.
D. Write the password to a temporary file with world-readable permissions.

Solution

  1. Step 1: Evaluate password storage methods

    Storing passwords in plain text inside scripts or passing them as command line arguments exposes them to other users or logs.
  2. Step 2: Choose secure handling

    Reading the password from a file with strict permissions (e.g., 600) and avoiding printing it prevents leaks and unauthorized access.
  3. Final Answer:

    Read the password from a file with restricted permissions and never echo it. -> Option A
  4. Quick Check:

    Restrict file permissions and avoid exposing secrets [OK]
Hint: Never expose passwords; restrict file access and avoid echoing [OK]
Common Mistakes:
  • Hardcoding passwords in scripts
  • Passing secrets as command line arguments
  • Using world-readable temporary files
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard