You can take advantage of the fact that |
is a special character in bash
, which means the %q
modifier used by printf
will escape it for you:
$ printf '%q\n' "text|jdbc"
text\|jdbc
A more general solution that doesn't require |
to be treated specially is
$ f="text|jdbc"
$ echo "${f//|/\\|}"
text\|jdbc
${f//foo/bar}
expands f
and replaces every occurance of foo
with bar
. The operator here is /
; when followed by another /
, it replaces all occurrences of the search pattern instead of just the first one. For example:
$ f="text|jdbc|two"
$ echo "${f/|/\\|}"
text\|jdbc|two
$ echo "${f//|/\\|}"
text\|jdbc\|two
_
as a delimiter:echo "text|jdbc" | sed -e 's_|_\\|_'
– Chiton