Search code examples
bashreplacebackslash

Replacing all backslash with double backslashes in bash


I'm trying to replace all \ with \\ in bash, I'm doing it like this but bash gets stuck in a never-ending loop. Where am I going wrong?

myVar="${myVar//\//\\\\}"

Solution

  • You can use sed for that:

    echo "hello\world\hello\world" | sed 's/\\/\\\\/g'
    

    Outputs:

    hello\\world\\hello\\world