70

With Bash and SED I'm trying to replace two strings in a js file with URL's.

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

./deploy.sh https://hostname.com/a/index.html https://hostname2.com/test

However to make this usable in my sed command I have to escape all forward slashes with: \\ ?

./deploy.sh https:\\/\\/hostname.com\\/a\\/index.html https:\\/\\/hostname2.com\\/test

If they are escaped this SED command works on Mac OSX Sierra

APP_URL=$1
API_URL=$2

sed "s/tempAppUrl/$APP_URL/g;s/tempApiUrl/$API_URL/g" index.src.js > index.js

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

This is what I've tried:

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 > index.js

The params looks the same but in this case the SED throws a error

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

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

Jonathan Andersson
  • 1,342
  • 3
  • 16
  • 31
  • It's not just the `/`s, there's a LOT more characters you have to escape (e.g. the `.`s in your current regexp) but sed won't give you an error message for most of them, it'll just corrupt your output. See http://stackoverflow.com/questions/29613304/is-it-possible-to-escape-regex-metacharacters-reliably-with-sed/29626460#29626460. If you want to learn how to do a robust string substitution then post a new question (and the answer will not be sed since sed does not support strings!). – Ed Morton Nov 21 '16 at 14:13

1 Answers1

177

I suggest to replace

sed "s/regex/replace/" file

with

sed "s|regex|replace|" file

if your sed supports it. Then it is no longer necessary to escape the slashes.

The character directly after the s determines which character is the separator, which must appear three times in the s command.

Cyrus
  • 84,225
  • 14
  • 89
  • 153
  • Hmm, The SED passed but with wrong result. https:\/\/hostname.com\/a\/index.html – Jonathan Andersson Nov 21 '16 at 07:44
  • 6
    If your sed supports it? Isn't that a feature of any sed since the epoch? – Jens Nov 21 '16 at 07:45
  • 4
    When using `|` as separator, you don't need to escape the slashes. Your `ESC_APP_URL=(${APP_URL//\//'\\/'})` lines are completely unnecessary and wrong. – Ipor Sircer Nov 21 '16 at 07:46
  • "If your sed supports it"? Which sed's do not? I have seen the differences in -i '' on mac vs Linux, but this seems to be a regex syntax adopted by more languages/applications, like perl. Can's seem to find much docs about the variant. But in both perl and sed it works on my mac (10.13). So thanks. – andiOak Apr 16 '22 at 11:25