[SOLVED] Question on shell scripting: grep with backslash
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I have a question about using backslash with grep. For searching -5 pattern in the below file.
[root@localhost script-practice]# cat phone.list
Smith, Terry 7-7989
Adams, Fran 2-3876
StClair, Pat 4-6122
Brown, Robin 1-3745
Stair, Chris 5-5972
Benson, Sam 4-5587
In the shell programming book by Ray Swartz, it is mentioned that to perform a grep operation for -5 pattern search we need to use \(backslash) before - to pass it to the grep as a character pattern to search but not as an option. It is mentioned that we straight away can't use grep \-5 phone.list instead we need to use grep \\-5 phone.list or grep '\-5' phone.list. Why do we have to use the second \ when single \ already takes care of quoting the following character - and removing its special meaning and treats it as a normal character. Please explain. Thanks in advance.
The command line is first processed by the shell. The result of an unquoted \- will just be -, and that is all that will be passed to grep, which will treat it as an option flag. The double \\ or a quoted '\' will result in a single \ being passed to grep.
But, this is really the wrong way to indicate that "-5" is not a flag option. The right way is
Code:
grep -- -5 phone list
The "--" is the standard way to indicate that no more option arguments follow, and that nothing that follows should be treated as an option.
Use single quotes to protect regex from the shell thus
Code:
grep '\-5' yourfile
Stair, Chris 5-5972
Benson, Sam 4-5587
Just to add to this comment the difference between single and double quote.
'123 $A$B$C xyz' verses "123 $A$B$C xyz"
In the single quotes the variable will get passed literally as $A$B$C, where as in the double quote the variables will get processed showing what information is stored and then passed.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.