Replace slash in Bash

user109447 picture user109447 · Jun 6, 2014 · Viewed 43.8k times · Source

Let's suppose I have this variable:

DATE="04/Jun/2014:15:54:26"

Therein I need to replace / with \/ in order to get the string:

"04\/Jun\/2014:15:54:26"

I tried tr as follows:

echo "04\Jun\2014:15:54:26" | tr  '\' '\\/'

But this results in: "04\Jun\2014:15:54:26".

It does not satisfy me. Can anyone help?

Answer

Luc-Olivier picture Luc-Olivier · Jun 6, 2014

No need to use an echo + a pipe + sed.

A simple substitution variable is enough and faster:

echo ${DATE//\//\\/}

#> 04\/Jun\/2014:15:54:26