single quote and double quotes in sed

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:

$ echo "We're good!" | sed "s/'re/ are/"
We are good!

$ name="John"; echo "Hello, I'm <name>." | sed "s/<name>/$name/"
Hello, I'm John.

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 what sed sees. When using double quotes, however, your string is going to be interpreted by the shell before being passed on to sed, and you must take into account any modifications, some of which might not be what you want. Here's a good example:

$ echo 'SELECT `date` FROM `services`' | sed 's/`date`/`uptime`/'
SELECT `uptime` FROM `services`

If you had used double quotes, here's what your actual sed command would have been like:

$ sed "s/`date`/`uptime`/"
$ sed 's/Wed May 29 08:28:20 UTC 2019/ 08:28:20 up 13 days,  1:29,  1 user,  load average: 0.08, 0.02, 0.00/'

You can often get away with double quotes in connection to sed, because use of the $ sign tends to be unambiguous:

$ line="back"; echo "front" | sed "s/front$/$line/"
back
$ line="back"; echo "frontline" | sed "s/front$/$line/"
frontline
$ line="-line"; echo "front-line" | sed "s/front$line/back$line/"
back-line

Things become more complicated if you're writing awk or perl, where $ has more uses:

$ echo "col1 col2 col3" | awk '{print $1}'
col1
$ echo "col1 col2 col3" | awk "{print $1}"
col1 col2 col3

$ echo "col1 col2 col3" | perl -lane 'print $F[0]'
col1
$ echo "col1 col2 col3" | perl -lane "print $F[0]"
ARRAY(0x564346f18db0)

This is actually related to bash, rather than just the sed command.

You can understand it better by looking at this example:

~ $ front="front"
~ $ echo "front" | sed "s/$front/back/"
back
~ $ echo "front" | sed 's/$front/back/'
front

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 is

$ echo "\alpha" | sed 's/\\alpha/\\beta/'
\beta

but for double quotes, we need three quotes,

$ echo "\alpha" | sed "s/\\alpha/\\beta/"
\alpha
$ echo "\alpha" | sed "s/\\\alpha/\\\beta/"
\beta

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,

$ echo "\alpha" | sed '/alpha/i \\\beta'
\beta
\alpha

The reason I am not sure, but according to the manual, there is indeed a \ after the command i,

       i \

       text   Insert text, which has each embedded newline preceded by a backslash.

While for double quotes, two more backslashes are needed,

$ echo "\alpha" | sed "/alpha/i \\\\\beta"
\beta
\alpha