what is meant by "^$1" in shell script?

8,847

grep is a program that searches for regular expressions. The first argument for grep is the pattern to look for. In scripts and functions $1 is a reference to the first argument passed to that script or function. The ^ prepended to the argument is a standard regular expressions modifier that matches the beginning of a line -- this way you can ensure that grep only prints lines in which the pattern you're looking for is placed immediately at the start of a new line and ignored if found elsewhere.

Please note that ^ can have an alternative meaning of negation, though not in this case here.

Share:
8,847
ghostloops
Author by

ghostloops

cs student

Updated on September 18, 2022

Comments

  • ghostloops
    ghostloops almost 2 years
    #!/bin/sh
    #emp3.sh: using if and else 
    #
    if grep "^$1" /etc/passwd 2>dev/null 
    then 
    echo "pattern found - job over"
    else 
    echo "pattern not found"
    fi 
    

    in the above programme what is "^$1" and is that same to "s1/*" can anyone explain this

    • jimmij
      jimmij about 9 years
      You should have space between "^$1" and /etc/passwd.