How to replace a string with a string containing slash with sed?
Use another character as delimiter in the s
command:
printf '%s\n' "$srcText" | sed "s|XPLACEHOLDERX|$connect|"
Or escape the slashes with ksh93's ${var//pattern/replacement}
parameter expansion operator (now also supported by zsh
, bash
, mksh
, yash
and recent versions of busybox sh
).
printf '%s\n' "$srcText" | sed "s/XPLACEHOLDERX/${connect//\//\\/}/"
If your shell supports it:
"${srcText/XPLACEHOLDERX/$connect}"