Escaping forward slashes in sed command
<p>With <code>Bash</code> and <code>SED</code> I'm trying to replace two strings in a js file with URL's.</p>

<p>The two urls that should be inserted is input params when I run the .sh script.</p>

<pre><code>./deploy.sh https://hostname.com/a/index.html https://hostname2.com/test
</code></pre>

<p>However to make this usable in my sed command I have to escape all forward slashes with: <code>\\</code> ?</p>

<pre><code>./deploy.sh https:\\/\\/hostname.com\\/a\\/index.html https:\\/\\/hostname2.com\\/test
</code></pre>

<p>If they are escaped this SED command works on Mac OSX Sierra</p>

<pre><code>APP_URL=$1
API_URL=$2

sed "s/tempAppUrl/$APP_URL/g;s/tempApiUrl/$API_URL/g" index.src.js &gt; index.js
</code></pre>

<p>Now I don't want to insert escaped urls as params, I want the script it self to escape the forward slashes.</p>

<p>This is what I've tried:</p>

<pre><code>APP_URL=$1
API_URL=$2

ESC_APP_URL=(${APP_URL//\//'\\/'})
ESC_API_URL=(${API_URL//\//'\\/'})

echo 'Escaped URLS'
echo $ESC_APP_URL
#Echos result: https:\\/\\/hostname.com\\/a\\/index.html 
echo $ESC_API_URL
#Echos result: https:\\/\\/hostname2.com\\/test

echo "Inserting app-URL and api-URL before dist"
sed "s/tempAppUrl/$ESC_APP_URL/g;s/tempApiUrl/$ESC_API_URL/g" index.src.js &gt; index.js
</code></pre>

<p>The params looks the same but in this case the SED throws a error </p>

<pre><code>sed: 1: "s/tempAppUrl/https:\\/\ ...": bad flag in substitute command: '\'
</code></pre>

<p>Could anyone tell me the difference here? The Strings looks the same but gives different results.</p>