Bird
Raised Fist0
Bash Scriptingscripting~20 mins

Script testing strategies in Bash Scripting - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Bash Script Testing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
Output of a simple test function in bash
What is the output of this bash script when run in a terminal?
Bash Scripting
#!/bin/bash

test_function() {
  if [ "$1" -gt 10 ]; then
    echo "Greater"
  else
    echo "Smaller or equal"
  fi
}

result=$(test_function 15)
echo "$result"
ASmaller or equal
BGreater
C15
DSyntax error
Attempts:
2 left
💡 Hint
Look at the condition inside the function and the argument passed.
🧠 Conceptual
intermediate
1:30remaining
Purpose of exit codes in bash script testing
Why are exit codes important when testing bash scripts?
AThey change the script's output text automatically
BThey prevent the script from running if errors occur
CThey indicate success or failure of commands, helping automate test checks
DThey format the script's output for readability
Attempts:
2 left
💡 Hint
Think about how scripts communicate results to other programs or tests.
🔧 Debug
advanced
2:00remaining
Identify the error in this test script snippet
What error will this bash script produce when run?
Bash Scripting
#!/bin/bash

check_file() {
  if [ -f $1 ]; then
    echo "File exists"
  else
    echo "File missing"
  fi
}

check_file "/tmp/testfile"
ANo error, outputs 'File exists' or 'File missing'
BSyntax error due to missing quotes around $1 in the if condition
CRuntime error: command not found
DOutputs nothing
Attempts:
2 left
💡 Hint
Check if the script handles file path arguments correctly.
🚀 Application
advanced
2:30remaining
Automate testing of a script output
You want to test if your script 'hello.sh' outputs exactly 'Hello World!'. Which command correctly tests this and returns exit code 0 if it matches?
Aif ./hello.sh | grep -q 'Hello World'; then echo 'Test failed'; else echo 'Test passed'; fi
Bif ./hello.sh | grep 'Hello'; then echo 'Test passed'; else echo 'Test failed'; fi
Cif ./hello.sh | grep -v 'Hello World!'; then echo 'Test passed'; else echo 'Test failed'; fi
Dif ./hello.sh | grep -q '^Hello World!$'; then echo 'Test passed'; else echo 'Test failed'; fi
Attempts:
2 left
💡 Hint
Use grep with exact match anchors and quiet mode to check output.
🧠 Conceptual
expert
3:00remaining
Best practice for isolating tests in bash scripts
Which approach best isolates tests to avoid side effects when running multiple bash script tests?
ARun each test in a separate subshell or temporary environment
BRun all tests sequentially in the same shell without resetting variables
CUse global variables to share state between tests
DRun tests only once to avoid repeated side effects
Attempts:
2 left
💡 Hint
Think about how to keep tests independent and avoid interference.

Practice

(1/5)
1. Why is it important to test a bash script before using it in real tasks?
easy
A. To reduce the script's file size
B. To make the script run faster automatically
C. To change the script's language to another programming language
D. To find and fix errors early, avoiding bigger problems later

Solution

  1. Step 1: Understand the purpose of testing scripts

    Testing helps catch mistakes before the script runs on important data or systems.
  2. Step 2: Identify the benefit of early error detection

    Finding errors early prevents failures and saves time and effort in fixing issues later.
  3. Final Answer:

    To find and fix errors early, avoiding bigger problems later -> Option D
  4. Quick Check:

    Testing = Catch errors early [OK]
Hint: Testing finds errors before damage happens [OK]
Common Mistakes:
  • Thinking testing makes scripts faster automatically
  • Confusing testing with rewriting code
  • Believing testing reduces file size
2. Which command is used to run a bash script with debugging output showing each command executed?
easy
A. bash -e script.sh
B. bash -v script.sh
C. bash -x script.sh
D. bash -c script.sh

Solution

  1. Step 1: Recall bash debugging options

    The -x option runs the script showing each command before execution.
  2. Step 2: Differentiate from other options

    -v prints shell input lines, -e exits on error, -c runs commands from string.
  3. Final Answer:

    bash -x script.sh -> Option C
  4. Quick Check:

    Debug with -x = bash -x [OK]
Hint: Use bash -x to see commands run step-by-step [OK]
Common Mistakes:
  • Confusing -v with -x for debugging
  • Using -e expecting debug output
  • Trying -c without command string
3. What will be the output of this script when run with bash -x test.sh?
#!/bin/bash
x=5
if [ $x -gt 3 ]; then
  echo "Greater"
else
  echo "Smaller"
fi
medium
A. + x=5 + '[' 5 -gt 3 ']' + echo 'Greater' Greater
B. Greater
C. + x=5 + echo 'Greater' Greater
D. Smaller

Solution

  1. Step 1: Understand bash -x output

    Running with -x prints each command with a plus sign before executing it.
  2. Step 2: Trace the script commands

    Commands x=5, the test [ $x -gt 3 ], and echo "Greater" are printed before output.
  3. Final Answer:

    + x=5 + '[' 5 -gt 3 ']' + echo 'Greater' Greater -> Option A
  4. Quick Check:

    bash -x shows commands with + [OK]
Hint: bash -x shows commands with + before output [OK]
Common Mistakes:
  • Ignoring the debug lines starting with +
  • Choosing output without debug info
  • Confusing else branch output
4. This script is intended to check if a file exists and print a message, but it fails. What is the error?
#!/bin/bash
filename="myfile.txt"
if [ -f $filename ]
then
  echo "File exists"
else
  echo "File missing"
fi
medium
A. The if statement syntax is incorrect; should use double brackets [[ ]]
B. Missing quotes around $filename can cause errors if filename has spaces
C. The script needs to use -e instead of -f to check file existence
D. The echo commands must be inside curly braces { }

Solution

  1. Step 1: Identify variable usage in test condition

    The variable $filename is unquoted, which can cause errors if it contains spaces.
  2. Step 2: Understand quoting importance in bash tests

    Quoting variables prevents word splitting and errors in test expressions.
  3. Final Answer:

    Missing quotes around $filename can cause errors if filename has spaces -> Option B
  4. Quick Check:

    Quote variables in tests = Missing quotes around $filename can cause errors if filename has spaces [OK]
Hint: Always quote variables in test conditions [OK]
Common Mistakes:
  • Thinking [[ ]] is required instead of [ ]
  • Confusing -f and -e options
  • Believing echo needs braces
5. You want to test a script that processes user input and exits with code 0 on success or 1 on failure. Which strategy best helps automate testing this behavior?
hard
A. Write test cases that run the script with different inputs and check the exit code using $?
B. Only run the script manually and watch the output on screen
C. Use echo statements inside the script without checking exit codes
D. Rename the script file to .test and run it without arguments

Solution

  1. Step 1: Understand exit codes in bash scripts

    Scripts use exit codes to signal success (0) or failure (non-zero).
  2. Step 2: Automate testing by running script with inputs and checking $?

    Running script with inputs and verifying $? confirms correct behavior automatically.
  3. Final Answer:

    Write test cases that run the script with different inputs and check the exit code using $? -> Option A
  4. Quick Check:

    Test exit codes with $? = Write test cases that run the script with different inputs and check the exit code using $? [OK]
Hint: Check $? after running script to verify success/failure [OK]
Common Mistakes:
  • Relying only on manual output checks
  • Ignoring exit codes in tests
  • Assuming renaming files triggers tests
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