Challenge - 5 Problems
Regex Master in Bash
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate2:00remaining
What is the output of this Bash script using regex with [[ ]]?
Consider the following Bash script snippet. What will it print when run?
Bash Scripting
input="hello123" if [[ $input =~ ^hello[0-9]+$ ]]; then echo "Match" else echo "No match" fi
Attempts:
2 left
💡 Hint
The regex checks if the string starts with 'hello' followed by one or more digits.
✗ Incorrect
The input string 'hello123' matches the regex ^hello[0-9]+$ because it starts with 'hello' and ends with digits only.
💻 Command Output
intermediate2:00remaining
What error does this Bash regex condition produce?
What happens when you run this Bash snippet?
Bash Scripting
input="test" if [[ $input =~ (test ]]; then echo "Yes" else echo "No" fi
Attempts:
2 left
💡 Hint
Look carefully at the regex pattern syntax inside [[ ]].
✗ Incorrect
The regex pattern '(test' is missing a closing parenthesis, causing a syntax error.
🚀 Application
advanced2:00remaining
Which option correctly checks if a variable contains only lowercase letters using [[ ]] and =~?
You want to check if the variable 'word' contains only lowercase letters a-z. Which Bash snippet does this correctly?
Attempts:
2 left
💡 Hint
The regex should match the whole string with only lowercase letters.
✗ Incorrect
Option C uses ^ and $ anchors with [a-z]+ to ensure the entire string is lowercase letters only.
🔧 Debug
advanced2:00remaining
Why does this Bash regex condition always fail?
Examine this code snippet:
input="abc123"
if [[ $input =~ "^[a-z]+[0-9]+$" ]]; then
echo "Match"
else
echo "No match"
fi
Why does it print "No match" even though the input fits the pattern?
Attempts:
2 left
💡 Hint
Check how quoting affects regex matching in [[ ]]
✗ Incorrect
Quoting the regex disables regex matching and treats it as a fixed string, so the match fails.
🧠 Conceptual
expert2:00remaining
What is the value of BASH_REMATCH after this regex match?
Given this Bash code:
text="user@example.com"
if [[ $text =~ ([a-z]+)@([a-z]+)\.com ]]; then
echo "${BASH_REMATCH[0]}"
echo "${BASH_REMATCH[1]}"
echo "${BASH_REMATCH[2]}"
fi
What will be printed?
Attempts:
2 left
💡 Hint
BASH_REMATCH[0] is the full match; subsequent indices are capture groups.
✗ Incorrect
BASH_REMATCH[0] holds the full matched string, [1] the first group (user), [2] the second group (example).