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)
$
sigil has a specific meaning in sed. – simlev May 28 '19 at 10:41$
, what the proofs for the sticking to single for an convention? @simlev – Wizard May 28 '19 at 10:58