Ubuntu – the difference between grep apple file and grep “apple” file

bashgrepsh

What is the difference between grep apple file and grep "apple" file? What does putting the quotation marks do? They both appear to work and do the exact same thing (display same line).

Best Answer

The quotation marks have an effect on what characters your shell considers special and to have a syntactic meaning. In your example this doesn't make a difference because apple contains no such characters.

But consider another example: grep apple tree file will search for the word apple in the files tree and file, whereas grep "apple tree" file will search the word apple tree in the file file. The quotation marks tell bash, that the word space in "apple tree" does not start a new parameter but shall be part of the current parameter. grep apple\ tree file would produce the same result, because \ tells bash to disregard the special meaning of the following character and treat it literally.