0
0
Bash Scriptingscripting~20 mins

Regex in [[ ]] with =~ in Bash Scripting - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Regex Master in Bash
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2: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
ARuntime error
BNo match
CSyntax error
DMatch
Attempts:
2 left
💡 Hint
The regex checks if the string starts with 'hello' followed by one or more digits.
💻 Command Output
intermediate
2: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
ASyntax error
BNo
CYes
DRuntime error
Attempts:
2 left
💡 Hint
Look carefully at the regex pattern syntax inside [[ ]].
🚀 Application
advanced
2: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?
Aif [[ $word =~ ^[A-Z]+$ ]]; then echo "Valid"; fi
Bif [[ $word =~ [a-z]+ ]]; then echo "Valid"; fi
Cif [[ $word =~ ^[a-z]+$ ]]; then echo "Valid"; fi
Dif [[ $word =~ ^[a-z]*$ ]]; then echo "Valid"; fi
Attempts:
2 left
💡 Hint
The regex should match the whole string with only lowercase letters.
🔧 Debug
advanced
2: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?
ABecause the regex is quoted, it is treated as a string, not a regex pattern
BBecause the input variable is not quoted
CBecause =~ does not support character classes
DBecause the regex is missing anchors
Attempts:
2 left
💡 Hint
Check how quoting affects regex matching in [[ ]]
🧠 Conceptual
expert
2: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?
A
user@example.com
user@example
com
B
user@example.com
user
example
C
user@example.com
example
user
DSyntax error
Attempts:
2 left
💡 Hint
BASH_REMATCH[0] is the full match; subsequent indices are capture groups.