Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

grep double quotes vs single quotes

Tags:

regex

grep

shell

I was surprised to find out that the single quote version works with regular expressions just the same. The only real difference I see now is that double quotes expand variables inside the regex pattern. Is there anything else I am missing?

like image 538
user3614293 Avatar asked Aug 06 '14 01:08

user3614293


People also ask

Do you need quotes for grep?

These special characters, called metacharacters, also have special meaning to the system and need to be quoted or escaped. Whenever you use a grep regular expression at the command prompt, surround it with quotes, or escape metacharacters (such as & ! . * $ ? and \ ) with a backslash ( \ ).

Is there a difference between single and double quotes?

General Usage Rules In America, Canada, Australia and New Zealand, the general rule is that double quotes are used to denote direct speech. Single quotes are used to enclose a quote within a quote, a quote within a headline, or a title within a quote.

What's the difference between a single quote and a double quote in bash?

If you're referring to what happens when you echo something, the single quotes will literally echo what you have between them, while the double quotes will evaluate variables between them and output the value of the variable.


1 Answers

The difference between single quotes and double quotes is a shell issue, not a grep issue. It is the shell that decides to do or not to do variable expansion before passing the arguments to grep. Because the last step in shell processing of arguments is quote removal, grep never even sees the quotes.

Variable expansion is not the only difference between single and double quotes. The shell also does command substitution and arithmetic expansion inside double quotes. For example:

$ echo "$(date) and 2+2=$((2+2))"
Tue Aug  5 18:52:39 PDT 2014 and 2+2=4
$ echo '$(date) and 2+2=$((2+2))'
$(date) and 2+2=$((2+2))
like image 94
John1024 Avatar answered Oct 01 '22 02:10

John1024