0
0
Bash Scriptingscripting~10 mins

Regex in [[ ]] with =~ in Bash Scripting - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to check if the variable 'text' contains the word 'hello' using regex.

Bash Scripting
if [[ $text [1] "hello" ]]; then
  echo "Match found"
fi
Drag options to blanks, or click blank then click option'
A=
B==
C=~
D!=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '==' instead of '=~' for regex matching.
Using single brackets [ ] which do not support '=~'.
2fill in blank
medium

Complete the code to check if the variable 'input' starts with a digit using regex.

Bash Scripting
if [[ $input [1] ^[0-9] ]]; then
  echo "Starts with a digit"
fi
Drag options to blanks, or click blank then click option'
A!=
B==
C=
D=~
Attempts:
3 left
💡 Hint
Common Mistakes
Using '==' instead of '=~' for regex.
Not escaping special characters when needed.
3fill in blank
hard

Fix the error in the code to correctly check if 'var' contains only letters.

Bash Scripting
if [[ $var [1] ^[a-zA-Z]+$ ]]; then
  echo "Only letters"
fi
Drag options to blanks, or click blank then click option'
A=~
B==
C=
D!=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '==' instead of '=~' causes the regex to be treated as a string.
Forgetting to quote the variable can cause errors if it is empty.
4fill in blank
hard

Fill both blanks to check if 'filename' ends with '.txt' using regex.

Bash Scripting
if [[ $filename [1] \.[2]$ ]]; then
  echo "Text file"
fi
Drag options to blanks, or click blank then click option'
A=~
Btxt
Ctxt$
D==
Attempts:
3 left
💡 Hint
Common Mistakes
Using '==' instead of '=~' for regex.
Not escaping the dot before 'txt'.
5fill in blank
hard

Fill all three blanks to create a regex that matches a string with exactly 3 digits.

Bash Scripting
if [[ $code [1] ^[2][3]$ ]]; then
  echo "Three digits"
fi
Drag options to blanks, or click blank then click option'
A=~
B[0-9]
C{3}
D==
Attempts:
3 left
💡 Hint
Common Mistakes
Using '==' instead of '=~'.
Forgetting to use curly braces for exact count.