This has not been said in other answers so I thought I'd add some clarifications:
tr
uses two sets of characters for replacement, and the characters from the first set are replaced with those from the second set in a one-to-one correspondance. The manpage states that
SET2 is extended to length of SET1 by repeating its last character as necessary. Excess characters of SET2 are ignored.
Example:
echo abca | tr ab de # produces decd
echo abca | tr a de # produces dbcd, 'e' is ignored
echo abca | tr ab d # produces ddcd, 'd' is interpreted as a replacement for 'b' too
When using sed
for substitutions, you can use another character than '/' for the delimiter, which will make your expression clearer (I like to use ':', @n34_panda proposed '#' in their answer). Don't forget to use the /g
modifier to replace all occurences: sed 's:/:\\/:g'
with quotes or sed s:/:\\\\/:g
without (backslashes have to be escaped twice).
Finally your shortest solution will probably be @Luc-Olivier's answer, involving substitution, in the following form (don't forget to escape forward slashes too when part of the expected pattern):
echo ${variable/expected/replacement} # will replace one occurrence
echo ${variable//expected/replacement} # will replace all occurrences
man tr
would tell you why it doesn't work. You could usesed
. – Poll