replace double quotes with back slash double quotes in shell script

shell

Solution

See the example:

$ P='"stackowerflow" is awesome'
$ echo $P                       
"stackowerflow" is awesome
$ echo ${P//\"/\\\"}            
\"stackowerflow\" is awesome

To escape `\` you have to use `\\`.

To escape `"` you have to use `\"`.

To escape `\"` you have to use combination of both: `\\\"`.

Problem

I am doing P=${P//\"/\\"} my expectation is ``` "stackoverflow" is awesome ``` changes to ``` \"stackoverflow\" is awesome ``` [edit] The updated syntax is still not running for ``` #!/bin/sh erroMsg=": Parse Error: line 2:122 mismatched input 'where' expecting EOF near '\"201305%\"'" echo $errorMsg echo ${errorMsg//\"\\\"} echo $errorMsg ``` Output is test.sh: 4: test.sh: Bad substitution

Original source