Bird
Raised Fist0
Bash Scriptingscripting~10 mins

Anchors (^, $) in Bash Scripting - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Anchors (^, $)
Start
↓
Input String
↓
Apply ^ anchor
↓
Match start of string?
No→No match
Yes↓
Apply $ anchor
↓
Match end of string?
No→No match
Yes↓
Full match found
↓
End
Anchors ^ and $ check if a pattern matches the start or end of a string in bash regex matching.
Execution Sample
Bash Scripting
echo "hello" | grep -E '^he'
echo "hello" | grep -E 'lo$'
Check if 'hello' starts with 'he' and ends with 'lo' using anchors.
Execution Table
StepInput StringPatternAnchor UsedMatch ResultOutput
1hello^he^Matches starthello
2hellolo$$Matches endhello
3hello^lo^No match
4hellohe$$No match
💡 No more patterns to test; matching ends.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4
Input Stringhellohellohellohellohello
Pattern^helo$^lohe$
Match ResultMatches startMatches endNo matchNo match
Outputhellohello
Key Moments - 2 Insights
Why does '^he' match 'hello' but '^lo' does not?
Because '^' means match only at the start of the string. 'hello' starts with 'he' but not with 'lo' (see execution_table rows 1 and 3).
Why does 'lo$' match 'hello' but 'he$' does not?
'$' means match only at the end of the string. 'hello' ends with 'lo' but not with 'he' (see execution_table rows 2 and 4).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the output when pattern '^he' is used on 'hello'?
A"hello"
B"" (empty)
C"he"
D"lo"
💡 Hint
Check row 1 in execution_table under Output column.
At which step does the pattern fail to match the input string?
AStep 1
BStep 2
CStep 3
DNo failure
💡 Hint
Look for 'No match' in Match Result column in execution_table.
If we change the pattern from '^he' to '^h', what would happen at Step 1?
ANo match
BMatches start
CMatches end
DMatches whole string
💡 Hint
'^h' still matches start of 'hello' as 'hello' starts with 'h' (see concept of ^ anchor).
Concept Snapshot
Anchors ^ and $ in bash regex check string positions.
^ matches start of string.
$ matches end of string.
Use with grep -E for pattern matching.
Example: '^he' matches strings starting with 'he'.
Example: 'lo$' matches strings ending with 'lo'.
Full Transcript
Anchors ^ and $ are special symbols in bash regex that let you check if a pattern is at the start or end of a string. The ^ anchor means the pattern must be at the very beginning. The $ anchor means the pattern must be at the very end. For example, using grep -E '^he' on the string 'hello' matches because 'hello' starts with 'he'. Using 'lo$' matches because 'hello' ends with 'lo'. If the pattern does not match at the required position, no output is returned. This helps you find exact matches at string edges.

Practice

(1/5)
1. What does the anchor ^ represent in a bash regular expression?
easy
A. It matches the end of a line.
B. It matches any digit.
C. It matches any whitespace character.
D. It matches the start of a line.

Solution

  1. Step 1: Understand the role of anchors in regex

    Anchors are special characters that match positions in text, not actual characters.
  2. Step 2: Identify the meaning of ^

    The ^ anchor matches the start of a line in bash regex.
  3. Final Answer:

    It matches the start of a line. -> Option D
  4. Quick Check:

    ^ means start of line [OK]
Hint: Remember ^ means start, $ means end of line [OK]
Common Mistakes:
  • Confusing ^ with end of line anchor
  • Thinking ^ matches a character instead of position
  • Mixing ^ with other regex symbols like * or .
2. Which of the following grep commands correctly finds lines that end with the word done?
easy
A. grep 'done$' file.txt
B. grep '^done' file.txt
C. grep 'done^' file.txt
D. grep '^done$' file.txt

Solution

  1. Step 1: Understand the meaning of $ anchor

    The $ anchor matches the end of a line in regex.
  2. Step 2: Match lines ending with 'done'

    Using done$ matches lines where 'done' is at the end.
  3. Final Answer:

    grep 'done$' file.txt -> Option A
  4. Quick Check:

    Use $ to match line end [OK]
Hint: Use $ after text to match line endings [OK]
Common Mistakes:
  • Using ^ after text instead of $
  • Using ^done to match line end
  • Confusing ^ and $ anchors
3. What will be the output of this command if file.txt contains the lines:
apple
banana
apple pie
pineapple
apple

grep '^apple$' file.txt
medium
A. apple apple pie apple
B. apple pie pineapple
C. apple apple
D. banana

Solution

  1. Step 1: Understand the regex ^apple$

    This matches lines that start and end exactly with 'apple' and nothing else.
  2. Step 2: Check each line in file.txt

    Lines 'apple' and 'apple' match exactly. 'apple pie' and 'pineapple' do not match because of extra text.
  3. Final Answer:

    apple apple -> Option C
  4. Quick Check:

    ^ and $ match exact whole line [OK]
Hint: Use ^ and $ together to match whole exact lines [OK]
Common Mistakes:
  • Assuming partial matches with anchors
  • Ignoring that $ matches end of line
  • Confusing lines with extra words as matches
4. You wrote this command to find lines starting with 'Error' in a log file:
grep 'Error^' logfile.txt
But it returns no results. What is the problem?
medium
A. The file does not contain 'Error'.
B. The anchor ^ should be at the start, not after 'Error'.
C. grep does not support anchors like ^.
D. The command needs double quotes instead of single quotes.

Solution

  1. Step 1: Analyze the position of the anchor ^

    The ^ anchor must be at the start of the pattern to match line start.
  2. Step 2: Correct the pattern placement

    Use ^Error to match lines starting with 'Error'. Placing ^ after 'Error' is invalid.
  3. Final Answer:

    The anchor ^ should be at the start, not after 'Error'. -> Option B
  4. Quick Check:

    ^ must be at pattern start [OK]
Hint: Place ^ only at pattern start to match line start [OK]
Common Mistakes:
  • Putting ^ after text instead of before
  • Assuming grep ignores anchors
  • Using wrong quotes causing shell issues
5. You want to extract lines from data.txt that start with a digit and end with a period. Which grep pattern correctly uses anchors to do this?
hard
A. grep '^[0-9].*\.$' data.txt
B. grep '^[0-9].*$' data.txt
C. grep '^\d.*\.$' data.txt
D. grep '^[0-9].*.$' data.txt

Solution

  1. Step 1: Match line start with a digit

    The pattern ^[0-9] matches lines starting with any digit.
  2. Step 2: Match line end with a literal period

    Use \.$ to match a literal '.' at the end. The dot must be escaped to avoid matching any character.
  3. Step 3: Combine pattern correctly

    ^[0-9].*\.$ matches lines starting with digit, any characters, ending with a period.
  4. Final Answer:

    grep '^[0-9].*\.$' data.txt -> Option A
  5. Quick Check:

    ^ start, $ end, escape . for literal dot [OK]
Hint: Escape dot to match literal period at line end [OK]
Common Mistakes:
  • Not escaping dot, matching any char instead of period
  • Using \d which grep may not support by default
  • Missing $ anchor at line end
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