Q:

Using grep to search texts with single quote?

I am using gedit text editor with embedded terminal in ubuntu 12.04. I'm trying to search for some text using grep. I want to search for this line of code 
'type' => 'select'

I tried:
grep -r '\'type\' => \'select\''
But grep didn't return any results.
So can someone kindly tell me how to search for the code above?

A:

Surround your search string with double quotes:
grep "'type' => 'select'"

A:

You cannot escape single quotes that appear within single quotes. As explained in the [bash manual]:(http://www.gnu.org/software/bash/manual/bashref.html#Single-Quotes):

Enclosing characters in single quotes (‘'’) preserves the literal value of each character within the quotes. A single quote may not occur between single quotes, even when preceded by a backslash.

So, you have to use different approaches:

Use double quotes:
grep  "'type' => 'select'" file 

If you prefer needlessly complex solutions:
grep  "'"type"'"\ =\>\ "'"select"'" file 

You can always search for any single character instead of specifying the single quotes:
grep  '.type. => .select.' file 

But just use ", it makes things much more straightforward.

