Bird
Raised Fist0
Bash Scriptingscripting~20 mins

[[ ]] extended test in Bash Scripting - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
[[ ]] Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
1:30remaining
Output of [[ ]] with string comparison
What is the output of the following bash script snippet?
Bash Scripting
str1="hello"
str2="hello"
if [[ $str1 == $str2 ]]; then
  echo "Match"
else
  echo "No Match"
fi
ASyntax Error
BNo Match
CMatch
DNo output
Attempts:
2 left
💡 Hint
The [[ ]] test compares strings for equality.
💻 Command Output
intermediate
1:30remaining
Numeric comparison with [[ ]]
What will this script output?
Bash Scripting
num=5
if [[ $num -gt 3 ]]; then
  echo "Greater"
else
  echo "Smaller or equal"
fi
ASyntax Error
BGreater
CSmaller or equal
DNo output
Attempts:
2 left
💡 Hint
Use -gt for numeric greater than comparison inside [[ ]].
💻 Command Output
advanced
1:30remaining
Testing file existence with [[ ]]
What is the output of this script if the file /etc/passwd exists?
Bash Scripting
if [[ -f /etc/passwd ]]; then
  echo "File exists"
else
  echo "File missing"
fi
AFile exists
BFile missing
CSyntax Error
DNo output
Attempts:
2 left
💡 Hint
The -f option tests if a file exists and is a regular file.
💻 Command Output
advanced
1:30remaining
Logical AND with [[ ]]
What will this script output?
Bash Scripting
a=10
b=20
if [[ $a -lt 15 && $b -gt 15 ]]; then
  echo "Both true"
else
  echo "Condition failed"
fi
ABoth true
BCondition failed
CSyntax Error
DNo output
Attempts:
2 left
💡 Hint
Use && inside [[ ]] for logical AND.
💻 Command Output
expert
2:00remaining
Complex string pattern matching with [[ ]]
What is the output of this script?
Bash Scripting
filename="report2024.txt"
if [[ $filename == report[0-9][0-9][0-9][0-9].txt ]]; then
  echo "Pattern matched"
else
  echo "No match"
fi
ASyntax Error
BNo match
CNo output
DPattern matched
Attempts:
2 left
💡 Hint
The [[ ]] supports pattern matching with glob patterns like [0-9].

Practice

(1/5)
1. What does the [[ ]] construct do in a bash script?
easy
A. It tests conditions like comparing strings or numbers.
B. It defines a function in bash.
C. It creates a new variable.
D. It runs a command in the background.

Solution

  1. Step 1: Understand the purpose of [[ ]]

    The [[ ]] syntax is used in bash to test conditions such as string comparison, number comparison, and file checks.
  2. Step 2: Compare with other options

    Options B, C, and D describe other bash features unrelated to [[ ]].
  3. Final Answer:

    It tests conditions like comparing strings or numbers. -> Option A
  4. Quick Check:

    [[ ]] = condition test [OK]
Hint: Remember: [[ ]] is for testing conditions in bash [OK]
Common Mistakes:
  • Confusing [[ ]] with function definition syntax
  • Thinking [[ ]] creates variables
  • Assuming [[ ]] runs commands
2. Which of the following is the correct syntax to check if variable var equals "hello" using [[ ]]?
easy
A. [[ $var != "hello" ]]
B. [[ $var == "hello" ]]
C. [[ $var === "hello" ]]
D. [[ $var -eq "hello" ]]

Solution

  1. Step 1: Identify correct string comparison operator in [[ ]]

    In bash, inside [[ ]], the operator for string equality is ==. -eq is for numeric equality. === is invalid.
  2. Step 2: Check each option

    [[ $var != "hello" ]] checks inequality. [[ $var -eq "hello" ]] uses numeric -eq on string (error). [[ $var === "hello" ]] uses invalid ===. [[ $var == "hello" ]] uses == correctly.
  3. Final Answer:

    [[ $var == "hello" ]] -> Option B
  4. Quick Check:

    String equality in [[ ]] uses == [OK]
Hint: Use == for string equality inside [[ ]] [OK]
Common Mistakes:
  • Using === which is invalid in bash
  • Using -eq for string comparison
  • Using != when checking for equality
3. What will be the output of this script snippet?
var=5
if [[ $var -gt 3 && $var -lt 10 ]]; then
echo "In range"
else
echo "Out of range"
fi
medium
A. In range
B. Out of range
C. Syntax error
D. No output

Solution

  1. Step 1: Evaluate numeric conditions inside [[ ]]

    The variable var is 5. The condition checks if 5 is greater than 3 and less than 10, which is true.
  2. Step 2: Determine which branch runs

    Since the condition is true, the script echoes "In range".
  3. Final Answer:

    In range -> Option A
  4. Quick Check:

    5 > 3 and 5 < 10 = true [OK]
Hint: Check each numeric condition carefully with && [OK]
Common Mistakes:
  • Confusing -gt and -lt operators
  • Using single & instead of &&
  • Forgetting to quote variables (not critical here)
4. Identify the error in this bash snippet:
filename="test.txt"
if [[ -f $filename && -r $filename ]]
then
echo "File exists and readable"
fi
medium
A. Variable $filename not quoted
B. Wrong operator for file check
C. Missing closing bracket "]" for [[ ]]
D. Missing then keyword

Solution

  1. Step 1: Check syntax of [[ ]] condition

    The condition starts with [[ and has matching closing ]]. So brackets are correct.
  2. Step 2: Verify other parts

    The then keyword is missing after the condition line, which causes a syntax error.
  3. Final Answer:

    Missing then keyword -> Option D
  4. Quick Check:

    Every if condition must be followed by then [OK]
Hint: Always include then after if condition [OK]
Common Mistakes:
  • Forgetting the then keyword
  • Using single [ with && without escaping
  • Ignoring variable quoting warnings
5. You want to check if a variable input is either "yes" or "y" (case insensitive) using [[ ]]. Which snippet correctly does this?
hard
A. [[ $input =~ ^(yes|y)$ ]]
B. [[ $input == "yes" || $input == "y" ]]
C. [[ ${input,,} == "yes" || ${input,,} == "y" ]]
D. [[ $input == "yes" && $input == "y" ]]

Solution

  1. Step 1: Understand case-insensitive check

    To ignore case, convert input to lowercase using ${input,,} before comparison.
  2. Step 2: Check logical OR condition

    We want to accept either "yes" or "y", so use || between comparisons.
  3. Step 3: Evaluate options

    [[ ${input,,} == "yes" || ${input,,} == "y" ]] correctly converts to lowercase and uses OR. [[ $input == "yes" || $input == "y" ]] lacks case conversion. [[ $input =~ ^(yes|y)$ ]] uses regex but is case-sensitive. [[ $input == "yes" && $input == "y" ]] uses AND which requires both to be true, impossible here.
  4. Final Answer:

    [[ ${input,,} == "yes" || ${input,,} == "y" ]] -> Option C
  5. Quick Check:

    Use ${var,,} for lowercase and || for OR [OK]
Hint: Use ${var,,} to lowercase and || for OR in [[ ]] [OK]
Common Mistakes:
  • Not converting input to lowercase
  • Using && instead of || for alternatives
  • Incorrect regex syntax inside [[ ]]
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