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?
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 ( \ ).
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.
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.
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))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With