Search code examples
stringbashescapingbackslashslash

how to escape file path in bash script variable


I would like to escape a file path that is stored in a variable in a bash script. I read several threads about escaping back ticks or but it seems not working as it should:

I have this variable: The variables value is entered during the bash script execution as user parameter

CONFIG="/home/teams/blabla/blabla.yaml"

I would need to change this to: \/home\/teams\/blabla\/blabla.yaml

How can I do that with in the script via sed or so (not manually)?


Solution

  • With GNU bash and its Parameter Expansion:

    echo "${CONFIG//\//\\/}"
    

    Output:

    \/home\/teams\/blabla\/blabla.yaml