0
0
Bash Scriptingscripting~5 mins

Anchors (^, $) in Bash Scripting - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the caret (^) anchor mean in a regular expression?
The caret (^) anchor means the pattern must match at the start of a line or string.
Click to reveal answer
beginner
What does the dollar sign ($) anchor mean in a regular expression?
The dollar sign ($) anchor means the pattern must match at the end of a line or string.
Click to reveal answer
beginner
In bash scripting, how would you use ^ and $ to match a line that exactly contains 'hello'?
Use the pattern '^hello$' to match lines that contain only 'hello' with nothing before or after.
Click to reveal answer
intermediate
Why are anchors useful in scripting when searching text?
Anchors help ensure you match text only at the start or end of lines, avoiding partial matches inside lines.
Click to reveal answer
beginner
What happens if you omit anchors in a regex pattern in bash?
Without anchors, the pattern can match anywhere in the line, which may lead to unexpected matches.
Click to reveal answer
What does the regex pattern '^abc' match?
ALines ending with 'abc'
BLines starting with 'abc'
CLines containing 'abc' anywhere
DLines exactly equal to 'abc'
What does the regex pattern 'xyz$' match?
ALines exactly equal to 'xyz'
BLines starting with 'xyz'
CLines containing 'xyz' anywhere
DLines ending with 'xyz'
Which pattern matches a line that contains only 'test' and nothing else?
A^test$
Btest
C^test
Dtest$
If you want to find lines that contain 'data' anywhere, which pattern should you use?
Adata
B^data
C^data$
Ddata$
What is the effect of using anchors in a grep command?
AMakes grep run faster
BIgnores case sensitivity
CLimits matches to start or end of lines
DMatches multiple lines at once
Explain how the ^ and $ anchors work in regular expressions and why they are useful in bash scripting.
Think about matching only at the start or end of a line.
You got /4 concepts.
    Describe a real-life example where using ^ and $ anchors in a bash script would help you find exactly what you want.
    Imagine searching a list for an exact name, not just part of it.
    You got /4 concepts.