3

This question already has an answer here:

trying to substitute single apostrophes to doubles. But cant get it right.

sed 's/'/"/'

marked as duplicate by don_crissti, Archemar, elbarna, DarkHeart, Bananguin Mar 12 '18 at 7:21

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

4
  echo "don't forget that" | sed 's/\x27/\"/'
2

With tr (if you want to do it globally in the whole file):

tr "'" '"' <infile >outfile

Doing

sed 's/'/"/'

You basically have sed 's/' followed by /"/', which is a slash and an unclosed double quoted string.

With

sed s/\'/\"/

you have no such problem.

If it feels better to quote the sed expression (it's not needed in this case), do

sed 's/'"'"'/"/'

This is sed 's/' followed by "'" (a double quoted single quote), followed by '/"/'.

  • To me tr \' \" looks better. – Hauke Laging Mar 10 '18 at 22:04
1

Single-quotes can't be escaped inside single quotes, so '\'' is the standard way of "embedding" a single-quote inside a single-quoted string in a shell command-line or script. It doesn't actually embed a single quote, but it achieves the desired end result.

'\'' = ' (end quote) \' (escaped quote) and ' (start quote).

In other words, instead of:

sed 's/'/"/g'

Use:

sed 's/'\''/"/g

Alternatively, double-quotes CAN be backslash-escaped inside double-quotes, so you could use:

sed "s/'/\"/g"

Be careful with this form - you have to escape shell meta-characters you want to be treated as string literals inside double quotes. e.g. sed 's/foo/$bar/' replaces foo with the string literal $bar, while sed "s/foo/$bar/" replaces foo with the value of the current shell's $bar variable (or with nothing if it isn't defined. Note: some values of variable $bar can break the sed command - e.g. if $bar contains an un-escaped delimiter like bar='a/b', that would cause the sed command to be s/foo/a/b/, a syntax error)

0

No need to quote the whole sed expression:

echo \' | sed s/\'/\"/

Not the answer you're looking for? Browse other questions tagged or ask your own question.