Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace slash in Bash

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?

like image 589
user109447 Avatar asked Jun 06 '14 08:06

user109447


People also ask

How do I change backslash to forward slash in Linux?

Press \/ to change every backslash to a forward slash, in the current line. Press \\ to change every forward slash to a backslash, in the current line.

How do I replace text in bash?

To replace content in a file, you must search for the particular file string. The 'sed' command is used to replace any string in a file using a bash script. This command can be used in various ways to replace the content of a file in bash. The 'awk' command can also be used to replace the string in a file.


1 Answers

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 
like image 83
Luc-Olivier Avatar answered Oct 03 '22 19:10

Luc-Olivier