Challenge - 5 Problems
[[ ]] Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate1: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
Attempts:
2 left
💡 Hint
The [[ ]] test compares strings for equality.
✗ Incorrect
The [[ $str1 == $str2 ]] condition checks if the two strings are equal. Since both are "hello", it prints "Match".
💻 Command Output
intermediate1: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
Attempts:
2 left
💡 Hint
Use -gt for numeric greater than comparison inside [[ ]].
✗ Incorrect
The condition [[ $num -gt 3 ]] checks if num is greater than 3. Since 5 > 3, it prints "Greater".
💻 Command Output
advanced1: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
Attempts:
2 left
💡 Hint
The -f option tests if a file exists and is a regular file.
✗ Incorrect
The [[ -f /etc/passwd ]] condition checks if the file exists and is a regular file. Usually, /etc/passwd exists, so it prints "File exists".
💻 Command Output
advanced1: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
Attempts:
2 left
💡 Hint
Use && inside [[ ]] for logical AND.
✗ Incorrect
The condition checks if a < 15 and b > 15. Since 10 < 15 and 20 > 15, both are true, so it prints "Both true".
💻 Command Output
expert2: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
Attempts:
2 left
💡 Hint
The [[ ]] supports pattern matching with glob patterns like [0-9].
✗ Incorrect
The pattern report[0-9][0-9][0-9][0-9].txt matches filenames starting with 'report' followed by exactly 4 digits and '.txt'. The filename matches, so it prints "Pattern matched".