Replacing "/\ characters with sed, from within php

Viewed 52

I generate password from php (thru web page) using shell_exec / dd/ /dev/urandom. I want to eliminate
"/\ characters from my generated password.

If I do a replacement in my sed line with

's/["/\]/!/g'

the script fails to execute. However I tested this from command line like

echo "this /is \"a \test" | sed -e 's/["/\]/!/g'

Then I get the right result : this !is !a !test

If I eliminate this replacement section from my sed line, php script does execute properly. What seems to be the issue? I thought characters within brackets [] is safe and does not need escaping.. is that the issue?

1 Answers

In PHP, if you use the command in a double quoted string literal, you need to escape the " with a single backslash, \", and the backslash inside the bracket expression must also be escaped.

In the end, this must look like

sed 's/[\"/\\]/!/g'
Related