how to replace “/” on path string with “\\/” using sed?
I've tried this: PATH="/user/dj/a/mydir" PATH_FORMAT=`echo "${PATH}" | sed 's/\//\\\//'` but it only replace the first "/". I want the resulting PATH_FORMAT value to be: "\/user\/dj\/a\/mydir" How can I do that? Add a g flag to your substitute command: echo "${PATH}" | sed 's/\//\\//g' Or more readable, as per Jonathan Wakely in the comments: echo "${PATH}" | sed 's:/:\\/:g' To achieve what you describe in your question, you need one more backslash: echo /user/dj/a/mydir | sed 's:/:\\\/:g' Output: \/user\/dj\/a\/mydir Use Shell's parameter expansion, if you are running a shell script: MYPATH="