grep for a backslash question


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users grep for a backslash question
# 1  
Old 08-04-2011
grep for a backslash question

Why does this work when grepping for a backslash?

Code:
grep '\\' .bash_history
grep "[\\]" .bash_history

Why does this not work when grepping for a backslash?

Code:
grep "\\" .bash_history

I know this works works but just don't understand why I need 4 backslashes when using double quotes.

Code:
grep "\\\\" .bash_history

# 2  
Old 08-04-2011
Try
Code:
$ echo '\\'
\\
$ echo "\\"
\
$

double-quotes use backslashes themselves to escape things like other double quotes.
# 3  
Old 08-04-2011
Quote:
Originally Posted by Corona688
Try
Code:
$ echo '\\'
\\
$ echo "\\"
\
$

double-quotes use backslashes themselves to escape things like other double quotes.
So single quotes take you literally and double quotes keep their special meaning?
# 4  
Old 08-04-2011
Quote:
So single quotes take you literally and double quotes keep their special meaning?
Essentially yes. Expansion does not happen within single quotes but does within double quotes.
# 5  
Old 08-04-2011
The grep command requires a \\ to mean \, also in shell's double quote and non-quote. So When you Use \\ or "\\" in shell, a \ will be passed to grep. Since \ is an escape char in grep, a single \ does not match any char. So If you use "\\\\", it works.
# 6  
Old 08-05-2011
Quote:
Originally Posted by fpmurphy
Essentially yes. Expansion does not happen within single quotes but does within double quotes.
And expansion turns two backslashes into a single backslash right?

Am I right to assume that there is expansion with this?

Code:
$ echo \\
\

How else do you prevent expansion besides with single quotes?

Quote:
Originally Posted by vistastar
The grep command requires a \\ to mean \, also in shell's double quote and non-quote. So When you Use \\ or "\\" in shell, a \ will be passed to grep. Since \ is an escape char in grep, a single \ does not match any char. So If you use "\\\\", it works.
Is this because of expansion like fpmurphy just mentioned?
# 7  
Old 08-06-2011
@ And expansion turns two backslashes into a single backslash right?
yes.

@ How else do you prevent expansion besides with single quotes?
You can't.

@ Is this because of expansion like fpmurphy just mentioned?
Yes.

IMO, the shell syntax is badly desinged, since they are confusing. But, they are rigorous. You can grasp them in practice.
see also:
[Chapter 7] 7.3 Command-line Processing
and the command 'eval'
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Grep Question

My grep returns a row of data like this: 75=20130130;60=074338;61=985;511=55473883;452=115439;62=196;267=1; Is there a way for the grep to only return 60="something" and 511="something" ? Thanks in advance. (10 Replies)
Discussion started by: Carl2013
10 Replies

2. UNIX for Dummies Questions & Answers

grep for a backslash as for loop parameter

Hello everyone, My main objective is to search for text within a file, namely a block of text where each line ends with a backslash "\". However, the block must begin with a keyword, like "loginstring". Here is an example of a file that contains a block: ############### loginstring \... (2 Replies)
Discussion started by: idlechatter
2 Replies

3. UNIX for Dummies Questions & Answers

Grep Question..

I am grepping a log file but want to filter out more than one thing.. tail -f log | grep -vi "FTP session" How can I add more filters to this string? Can I do tail -f log | grep -vi "FTP session" | grep -vi "xxx" or is there a better method. Thanks Use code tags please, ty. (7 Replies)
Discussion started by: NelsonC
7 Replies

4. UNIX for Dummies Questions & Answers

grep question

Instead of using the following command #dmesg | grep -v sendmail | grep -v xntpd How can I use just one grep -v and give both arguments. Please suggest thanks (4 Replies)
Discussion started by: Tirmazi
4 Replies

5. UNIX for Dummies Questions & Answers

grep question

I wanted to search a for all lines containing ERROR but not errors that contained the word "foo" (for example). The only way I could figure out to do it was: grep ERROR myfile.log | grep -v foo is there a way to do this with one grep command instead of two? One grep is faster than two,... (4 Replies)
Discussion started by: tim-bobby
4 Replies

6. UNIX for Advanced & Expert Users

How to handle backslash in grep string

Hi , I am doing invert grep using -v but the string contain "/" which break the grep command and it do not skip the lines with "/" on it. Diffu.txt ======== 1159c1159 < <td align="right" valign="middle" class="paddingRight2px" id="featureListItemChannelButton7466"> --- > <td... (6 Replies)
Discussion started by: rajbal
6 Replies

7. Shell Programming and Scripting

How ro handle backslash character in grep?

Hi , I am doing invert grep using -v but the string contain "/" which break the grep command and it do not skip the lines with "/" on it. Diffu.txt ======== 1159c1159 < <td align="right" valign="middle" class="paddingRight2px" id="featureListItemChannelButton7466"> --- > <td... (1 Reply)
Discussion started by: rajbal
1 Replies

8. UNIX for Dummies Questions & Answers

grep -f question

when using grep -f file1 file2 if you have multiple entries in the pattern file1 that are the same will it take the line out of file2 that matches file1 each time it comes up? if not by default can you set a flag to make this possible? or another way - can you get it to search for and match the... (8 Replies)
Discussion started by: Adriel
8 Replies

9. UNIX for Dummies Questions & Answers

Question about GREP

I have 2 files, in one file is a list of hex numbers, the other is what i need matched. Is it possible for me to specify to grep the list and have it go through the second file for each item in the list in the first file to match the lines? so this is like a cross-refference. (16 Replies)
Discussion started by: Adriel
16 Replies

10. Shell Programming and Scripting

Grep question

I'm using grep in a shell and I was wondering: Can I grep a file and then delete all files that contain what it returns? So instead of grep 'blah' * and I have 50 files that have blah in it and I would have to delete all 50 manually, how would I just delete them all in one fell swoop? (3 Replies)
Discussion started by: tphegley
3 Replies
Login or Register to Ask a Question