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?
Asked
Active
Viewed 7,759 times
14
-
1The general answer is at https://stackoverflow.com/questions/6697753/difference-between-single-and-double-quotes-in-bash. – Davis Herring Sep 18 '17 at 00:40
1 Answers
21
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))

John1024
- 109,961
- 14
- 137
- 171