single quote and double quotes in sed
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/'
backThey 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?
53 Answers
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-lineThings 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/'
frontDouble quotes evaluate the expression inside, whereas single quotes preserve the literal value of each character. More info here:
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/'
\betabut for double quotes, we need three quotes,
$ echo "\alpha" | sed "s/\\alpha/\\beta/"
\alpha
$ echo "\alpha" | sed "s/\\\alpha/\\\beta/"
\betasince 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
\alphaThe 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 More in general
"Zoraya ter Beek, age 29, just died by assisted suicide in the Netherlands. She was physically healthy, but psychologically depressed. It's an abomination that an entire society would actively facilitate, even encourage, someone ending their own life because they had no hope. Th…"