Search for / and insert \ for every instance

Hi,

I want to do a sed (linux) or the alternative in PHP to insert a backslash in front of every occurrence of every forward slash

ex.

/archive/data/stanley
-->
\/archive\/data\/stanley

I appreciate it!

sed 's#\/#\\\/#g'

OR

sed 's#\/#\\&#g'
1 Like

If i want to assign post result in shell script as:
assigning the literal string to a variable ..

echo $VERSION | sed 's#\/#\\\/#g' > $MOD_VERSION

What's the way to make that work?

/ is only special when it's used as the delimiter.

The highlighted backslashes are incorrect and produce undefined escape sequences.

POSIX sed:

Regards,
Alister

You don't need sed for that in bash or ksh:

VERSION="${VERSION//\//\\/}"

if you are forced to use an old shell:

VERSION=$(echo "$VERSION" | sed 's#/#\\/#g'
1 Like