Skip to main content
added 106 characters in body
Source Link
choroba
  • 49.4k
  • 7
  • 92
  • 118

In case of the certificate, there'sthere are only onetwo problematic charactercharacters: a slash and newline. You can use parameter expansion to replace itthem by \/ and \n whichrespectivelly which sed understands:

sed "s/text/$var=${var//$'\n'/\\n/}
var=${var//\//\\\/}
sed "s/text/$var/"

But in general, I'd use Perl which knows which characters come from a variable and what characters are part of the syntax:

var=$var perl -pe 's/text/$ENV{var}/'

In case of the certificate, there's only one problematic character: a newline. You can replace it by \n which sed understands:

sed "s/text/${var//$'\n'/\\n}/"

But in general, I'd use Perl which knows which characters come from a variable and what characters are part of the syntax:

var=$var perl -pe 's/text/$ENV{var}/'

In case of the certificate, there are only two problematic characters: a slash and newline. You can use parameter expansion to replace them by \/ and \n respectivelly which sed understands:

var=${var//$'\n'/\\n/}
var=${var//\//\\\/}
sed "s/text/$var/"

But in general, I'd use Perl which knows which characters come from a variable and what characters are part of the syntax:

var=$var perl -pe 's/text/$ENV{var}/'
Source Link
choroba
  • 49.4k
  • 7
  • 92
  • 118

In case of the certificate, there's only one problematic character: a newline. You can replace it by \n which sed understands:

sed "s/text/${var//$'\n'/\\n}/"

But in general, I'd use Perl which knows which characters come from a variable and what characters are part of the syntax:

var=$var perl -pe 's/text/$ENV{var}/'