How to grep for the pattern at the beginning of the line

String at the beginning of a line can be grep-ed by prefixing a caret(^) symbol to the pattern.

grep ^pattern My-Filename
# (^), caret symbol

Here is an example

# Example output:
ubuntu@codetryout:~$ grep ^ubuntu /etc/passwd

Grep pattern at the beginning of the line, case insensitive

ubuntu@codetryout:~$ grep -i ^UBUNTU /etc/passwd

Grep output to display the line number in result

ubuntu@codetryout:~$ grep -in ^UBUNTU /etc/passwd

Here is another example with multiple output lines, grep-ing for lines starting with the character “a

ubuntu@codetryout:~$ grep -in ^a /etc/passwd
28:avahi-autoipd:x:109:116:Avahi autoip daemon,,,:/var/lib/avahi-autoipd:/usr/sbin/nologin
34:avahi:x:115:121:Avahi mDNS daemon,,,:/var/run/avahi-daemon:/usr/sbin/nologin

We have just explored several ways to grep pattern at the beginning of a text file