Bird
Raised Fist0
Bash Scriptingscripting~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What does the expression [[ "hello123" =~ [a-z]+[0-9]+ ]] check in a bash script?
easy
A. If the string is exactly 'hello123'
B. If the string contains letters followed by numbers
C. If the string contains only numbers
D. If the string starts with a number

Solution

  1. Step 1: Understand the regex pattern

    The pattern [a-z]+[0-9]+ means one or more lowercase letters followed by one or more digits.
  2. Step 2: Apply the pattern to the string

    The string "hello123" starts with letters "hello" and ends with digits "123", so it matches the pattern.
  3. Final Answer:

    If the string contains letters followed by numbers -> Option B
  4. Quick Check:

    Regex matches letters then digits = A [OK]
Hint: Look for letter-digit sequence in string with regex [OK]
Common Mistakes:
  • Thinking quotes are needed around regex inside [[ ]]
  • Confusing exact match with pattern match
  • Assuming regex matches anywhere without order
2. Which of the following is the correct syntax to test if variable var matches regex ^[0-9]+$ in bash?
easy
A. [[ $var =~ "^[0-9]+$" ]]
B. [[ "$var" =~^[0-9]+$ ]]
C. [[ $var =~ ^[0-9]+$ ]]
D. [[ "$var" =~ "^[0-9]+$" ]]

Solution

  1. Step 1: Recall regex syntax in [[ ]]

    Inside [[ ]], the regex pattern should NOT be quoted to work correctly.
  2. Step 2: Check each option

    [[ $var =~ ^[0-9]+$ ]] uses [[ $var =~ ^[0-9]+$ ]] without quotes around regex, which is correct syntax.
  3. Final Answer:

    [[ $var =~ ^[0-9]+$ ]] -> Option C
  4. Quick Check:

    No quotes around regex pattern = B [OK]
Hint: Do not quote regex pattern inside [[ ]] [OK]
Common Mistakes:
  • Putting quotes around regex pattern causing syntax errors
  • Quoting variable but not regex or vice versa
  • Using single brackets [ ] instead of [[ ]]
3. What will be the output of this bash script snippet?
text="abc123"
if [[ $text =~ ^[a-z]+[0-9]+$ ]]; then
  echo "Match"
else
  echo "No match"
fi
medium
A. Match
B. No match
C. Syntax error
D. Empty output

Solution

  1. Step 1: Analyze the regex pattern

    The pattern ^[a-z]+[0-9]+$ means the string must start with one or more lowercase letters and end with one or more digits, with nothing else.
  2. Step 2: Check the string against the pattern

    The string "abc123" starts with letters "abc" and ends with digits "123", so it matches the pattern exactly.
  3. Final Answer:

    Match -> Option A
  4. Quick Check:

    String matches regex pattern = D [OK]
Hint: Check start ^ and end $ anchors carefully [OK]
Common Mistakes:
  • Ignoring anchors ^ and $ in regex
  • Assuming partial match is enough
  • Quoting regex pattern causing failure
4. Identify the error in this bash snippet:
input="2023-04-01"
if [[ "$input" =~ "^[0-9]{4}-[0-9]{2}-[0-9]{2}$" ]]; then
  echo "Valid date"
else
  echo "Invalid date"
fi
medium
A. Quotes around regex cause the match to fail
B. Variable input is not quoted
C. Regex pattern is invalid syntax
D. Missing semicolon after if condition

Solution

  1. Step 1: Check quoting rules for regex in [[ ]]

    Regex pattern inside [[ ]] with =~ must NOT be quoted; quotes make it a string literal, breaking regex matching.
  2. Step 2: Identify the error in the snippet

    The snippet quotes the regex pattern, causing the condition to always fail and print "Invalid date".
  3. Final Answer:

    Quotes around regex cause the match to fail -> Option A
  4. Quick Check:

    No quotes around regex pattern = C [OK]
Hint: Never quote regex pattern inside [[ ]] [OK]
Common Mistakes:
  • Quoting regex pattern inside [[ ]]
  • Forgetting to quote variable when needed
  • Assuming regex syntax errors without checking quotes
5. You want to check if a variable filename ends with either ".jpg" or ".png" using regex in bash. Which condition correctly tests this?
hard
A. [[ "$filename" =~ "\.(jpg|png)$" ]]
B. [[ $filename =~ "\.(jpg|png)$" ]]
C. [[ "$filename" =~\.(jpg|png)$ ]]
D. [[ $filename =~ \.(jpg|png)$ ]]

Solution

  1. Step 1: Understand regex for file extensions

    The pattern \.(jpg|png)$ matches a dot followed by 'jpg' or 'png' at the end of the string.
  2. Step 2: Apply correct quoting rules

    Regex pattern must NOT be quoted inside [[ ]], and variable can be unquoted if it has no spaces. [[ $filename =~ \.(jpg|png)$ ]] correctly uses unquoted regex and variable.
  3. Final Answer:

    [[ $filename =~ \.(jpg|png)$ ]] -> Option D
  4. Quick Check:

    Unquoted regex with alternation = A [OK]
Hint: Use unquoted regex with alternation inside [[ ]] [OK]
Common Mistakes:
  • Quoting regex pattern causing failure
  • Not escaping dot before extension
  • Quoting variable unnecessarily
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