Bird
Raised Fist0
Bash Scriptingscripting~20 mins

Why regex enables pattern matching in Bash Scripting - Challenge Your Understanding

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Regex Pattern Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
What is the main reason regex enables pattern matching?
Why does regex allow scripts to find patterns in text effectively?
ABecause regex converts text into numbers for faster comparison
BBecause regex defines flexible rules to describe text patterns using special symbols
CBecause regex stores all text in memory for quick searching
DBecause regex uses machine learning to guess text patterns
Attempts:
2 left
💡 Hint
Think about how regex uses symbols like * and ? to match text.
💻 Command Output
intermediate
2:00remaining
What is the output of this grep command using regex?
Given a file 'data.txt' with lines: 'apple', 'application', 'banana', what does this command output? grep '^app.*' data.txt
Bash Scripting
apple
application
banana
A
application
banana
Bbanana
Capple
D
apple
application
Attempts:
2 left
💡 Hint
The regex '^app.*' matches lines starting with 'app'.
📝 Syntax
advanced
2:00remaining
Which regex pattern correctly matches an email address in bash?
Choose the regex pattern that matches a simple email format like 'user@example.com' in bash scripts.
A'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$'
B'[a-z]+@[a-z]+\.[a-z]+'
C'^\w+@\w+\.\w{2,3}$'
D'[a-zA-Z]+@[0-9]+\.[a-z]{2,}'
Attempts:
2 left
💡 Hint
Look for a pattern that allows letters, numbers, dots, and special characters before and after '@'.
🔧 Debug
advanced
2:00remaining
Why does this regex fail to match 'file123.txt'?
The regex '^file[0-9]+\.txt$' is used but does not match 'file123.txt'. What is the likely cause?
Bash Scripting
echo 'file123.txt' | grep '^file[0-9]+\.txt$'
AThe caret '^' is misplaced and should be at the end
BThe dot '.' is not escaped properly, so it matches any character
CThe '+' symbol is not recognized by grep without -E option, causing no match
DThe brackets [] are used incorrectly for digits
Attempts:
2 left
💡 Hint
Basic grep does not support '+' quantifier without extra flags.
🚀 Application
expert
3:00remaining
How to extract all dates in YYYY-MM-DD format from a text file using bash regex?
You want to find all dates like '2023-06-15' in a file 'log.txt'. Which command correctly extracts these dates?
Agrep -oE '\b[0-9]{4}-[0-9]{2}-[0-9]{2}\b' log.txt
Bgrep '\d{4}-\d{2}-\d{2}' log.txt
Cgrep -E '[0-9]{4}/[0-9]{2}/[0-9]{2}' log.txt
Dgrep -o '[0-9]{4}-[0-9]{2}-[0-9]{2}' log.txt
Attempts:
2 left
💡 Hint
Use extended regex and word boundaries to match exact date format.

Practice

(1/5)
1. Why is regex useful in bash scripting for pattern matching?
easy
A. It speeds up the computer hardware.
B. It automatically fixes syntax errors.
C. It replaces the need for variables in scripts.
D. It allows searching for complex text patterns easily.

Solution

  1. Step 1: Understand regex purpose

    Regex is designed to find patterns in text, not to affect hardware or fix code.
  2. Step 2: Connect regex to bash scripting

    In bash, regex helps scripts find complex text patterns easily, making searches smarter.
  3. Final Answer:

    It allows searching for complex text patterns easily. -> Option D
  4. Quick Check:

    Regex = complex pattern search [OK]
Hint: Regex = smart text search tool in scripts [OK]
Common Mistakes:
  • Thinking regex speeds up hardware
  • Confusing regex with variable usage
  • Believing regex fixes code errors automatically
2. Which of the following is the correct way to use regex in a bash if statement?
easy
A. if [[ $var =~ ^abc ]]; then echo 'Match'; fi
B. if [ $var == ^abc ]; then echo 'Match'; fi
C. if [[ $var =* abc ]]; then echo 'Match'; fi
D. if ($var =~ ^abc) { echo 'Match'; }

Solution

  1. Step 1: Identify correct regex syntax in bash

    Bash uses double brackets [[ ]] with =~ operator for regex matching.
  2. Step 2: Check each option

    if [[ $var =~ ^abc ]]; then echo 'Match'; fi uses [[ $var =~ ^abc ]] which is correct. Others use wrong syntax or operators.
  3. Final Answer:

    if [[ $var =~ ^abc ]]; then echo 'Match'; fi -> Option A
  4. Quick Check:

    Use [[ ]] and =~ for regex in bash [OK]
Hint: Use [[ ]] with =~ for regex in bash [OK]
Common Mistakes:
  • Using single brackets [ ] with =~
  • Using == for regex instead of =~
  • Using shell or other language syntax
3. What will be the output of this bash script snippet?
text="hello123"
if [[ $text =~ ^hello[0-9]+$ ]]; then
echo "Matched"
else
echo "No match"
fi
medium
A. No match
B. Empty output
C. Matched
D. Syntax error

Solution

  1. Step 1: Analyze regex pattern

    The pattern ^hello[0-9]+$ means the string must start with 'hello' followed by one or more digits and end there.
  2. Step 2: Check the variable value

    text="hello123" matches the pattern because '123' are digits after 'hello'.
  3. Final Answer:

    Matched -> Option C
  4. Quick Check:

    "hello123" matches ^hello[0-9]+$ [OK]
Hint: Check regex anchors ^ and $ carefully [OK]
Common Mistakes:
  • Ignoring start ^ and end $ anchors
  • Misreading + as zero or more
  • Confusing digits with letters
4. Identify the error in this bash regex usage:
if [ $input =~ ^test ]; then echo "Yes"; fi
medium
A. Missing quotes around $input
B. Using single brackets [ ] instead of double brackets [[ ]]
C. Regex pattern is invalid
D. Echo command syntax is wrong

Solution

  1. Step 1: Check brackets for regex

    Bash requires double brackets [[ ]] to use the =~ regex operator correctly.
  2. Step 2: Verify other parts

    Quotes around $input are optional but recommended; regex pattern and echo syntax are fine.
  3. Final Answer:

    Using single brackets [ ] instead of double brackets [[ ]] -> Option B
  4. Quick Check:

    Use [[ ]] for regex matching [OK]
Hint: Use [[ ]] not [ ] for regex matching [OK]
Common Mistakes:
  • Using single brackets with =~
  • Forgetting to quote variables (less critical here)
  • Assuming regex pattern is wrong
5. You want to filter lines in a file that start with 'Error' followed by any digits using grep. Which command correctly uses regex for this?
hard
A. grep '^Error[0-9]\+' filename.txt
B. grep 'Error[0-9]+' filename.txt
C. grep '^Error[0-9]*' filename.txt
D. grep '^Error[0-9]+' filename.txt

Solution

  1. Step 1: Understand grep regex types

    Basic grep uses basic regex where + needs to be escaped as \+ to mean one or more.
  2. Step 2: Analyze each option

    grep '^Error[0-9]\+' filename.txt uses '^Error[0-9]\+' which matches lines starting with 'Error' followed by one or more digits correctly.
  3. Final Answer:

    grep '^Error[0-9]\+' filename.txt -> Option A
  4. Quick Check:

    Escape + as \+ in basic grep regex [OK]
Hint: Escape + as \+ in basic grep regex [OK]
Common Mistakes:
  • Using + without escape in basic grep
  • Using * instead of + for one or more
  • Missing ^ anchor for line start
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