I tested the single quote and double quotes in sed
me@host:~:
$ echo "front" | sed "s/front/back/"
back
me@host:~:
$ echo "front" | sed 's/front/back/'
back
They performed identically, but I remembered to read there exist differences between them months ago.
Google search not helpful with keywords "single quotes and double quotes in sed".
They are not identically, right?
I'm expanding my earlier comments into an answer.
My advice would be sticking to single quotes, unless you know what you're doing and need to take advantage of some command-line functionality. The most common cases that come to mind are single quotes inside text strings and shell variable interpolation.
Here's a couple of examples where using double quotes makes perfectly sense:
It seems that double quotes enable some nice functionality, so why not always use them? The main reason for me is not always seeing what command is actually being executed by
sed
. It's very simple when the command is enclosed in single quotes: what you see is exactly whatsed
sees. When using double quotes, however, your string is going to be interpreted by the shell before being passed on tosed
, and you must take into account any modifications, some of which might not be what you want. Here's a good example:If you had used double quotes, here's what your actual
sed
command would have been like:You can often get away with double quotes in connection to
sed
, because use of the$
sign tends to be unambiguous:Things become more complicated if you're writing
awk
orperl
, where$
has more uses:This is actually related to bash, rather than just the
sed
command.You can understand it better by looking at this example:
Double quotes evaluate the expression inside, whereas single quotes preserve the literal value of each character. More info here: https://stackoverflow.com/questions/6697753/difference-between-single-and-double-quotes-in-bash
Just share one more example to illustrate the different results caused by single and double quotes.
When there is a backslash, single
\
is not enough, and another one\
is needed to escape\
, that isbut for double quotes, we need three quotes,
since one
\
has been used up when interpreting in the shell.Moreover, if we want to insert before the given string, one more
\
is necessary,The reason I am not sure, but according to the manual, there is indeed a
\
after the commandi
,While for double quotes, two more backslashes are needed,