1

The following command fails.

sed 's/user=\'mysql\'/user=`whoami`/g' input_file

An example input_file contains the following line

user='mysql'

The corresponding expected output is

user=`whoami`

(Yes, I literally want whoami between backticks, I don't want it to expand my userid.)

Enlico
  • 23,259
  • 6
  • 48
  • 102
sunknudsen
  • 6,356
  • 3
  • 39
  • 76
  • Maybe this is helpful: [`sed` command with `i` option failing on Mac but works on Linux](https://stackoverflow.com/questions/4247068/sed-command-with-i-option-failing-on-mac-but-works-on-linux) – lurker Jan 27 '20 at 19:52
  • 1
    You can't insert a sinqle quote in a single quoted string, and single quotes prevent all expansions including command substitutions (the `\`whoami\`` part). Use double quotes around the sed command. – Benjamin W. Jan 27 '20 at 20:19
  • Thanks for the feedback @BenjaminW.. I am not trying to substitute `whoami` but rather replace "user='mysql'" with "user=`whoami`". Isn’t it possible to escape single quotes using backslashes? – sunknudsen Jan 27 '20 at 20:20
  • Can you show input and expected output? You want to insert a literal `\`whoami\``, if I understand correctly. As for single quotes within single quotes: see [here](https://stackoverflow.com/q/1250079/3266847). – Benjamin W. Jan 27 '20 at 20:41
  • @BenjaminW.. Please see edit. Thanks for your help! – sunknudsen Jan 27 '20 at 20:46

1 Answers1

1

This should be what you need:

  • Using double quotes to enclose the sed command,
  • so that you are free to use single quotes in it;
  • escape backticks to avoid the expansion.
sed "s/user='mysql'/user=\`whoami\`/g" yourfile

I've intentionally omitted the -i option for the simple reason that it is not part of the issue.

To clarify the relation between single quotes and escaping, compare the following two commands

  • echo 'I didn\'t know'
  • echo 'I didn'\''t know'

The former will wait for further input as there's an open ', whereas the latter will work fine, as you are concatenating a single quoted string ('I didn'), an escaped single quote (\'), and another single quoted string ('t know').

Enlico
  • 23,259
  • 6
  • 48
  • 102
  • So it’s not possible to escape single quotes using a backslashes? – sunknudsen Jan 27 '20 at 20:57
  • Thanks for your help! I guess I am mislead by things like `echo "Here’s a \" which has been escaped"`. – sunknudsen Jan 27 '20 at 21:05
  • Yes, double quotes and single quotes are very different. Btw, I've rejected your edit for the simple reason that the not the question, nor the answer, have anything specific to MacOS. I would rather suggest that you remove that detail from your own question, as it could just mislead the reader to think that something about this question is special in MacOS. – Enlico Jan 27 '20 at 21:09
  • 2
    @sunknudsen The handling of quotes is done by the shell; if you use Bash, the [manual](https://www.gnu.org/software/bash/manual/bash.html#Single-Quotes) says "A single quote may not occur between single quotes, even when preceded by a backslash.", and the [POSIX spec](https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_02_02) says "A single-quote cannot occur within single-quotes." – Benjamin W. Jan 27 '20 at 21:23