sed Delimiters: Use | or # Instead of /
sed accepts any single character as the s command delimiter, so editing paths is far easier with | or # than escaping every slash.
The slash in s/old/new/ is just the default delimiter. Pick a character that does not appear in your text and slash-heavy paths stop needing backslashes.
What it does
The character right after the s sets the delimiter for that command. sed uses the same character to separate the pattern, the replacement, and the flags. Choosing a delimiter absent from your data removes the need to escape it.
Common usage
# slashes everywhere: pick | as delimiter
sed 's|/usr/local/bin|/opt/bin|' file
# # works too
sed 's#http://old#https://new#g' urls.txt
# the same edit with / forces ugly escaping
sed 's/\/usr\/local\/bin/\/opt\/bin/' fileOptions
| Delimiter | When to use it |
|---|---|
| / | Default; fine when the text has no slashes |
| | | Good for file paths and URLs |
| # | Common alternative; avoid if text has # |
| s,old,new, | Comma delimiter works as well |
| any char | Any single char that is not in the pattern or value |
| \ in address | Use \cregexc to set a delimiter for an address too |
In CI
Delimiter choice is portable across GNU and BSD sed and is the cleanest fix for path substitutions in deploy scripts. The character must not appear unescaped inside the pattern or replacement, so pick one your data never uses.
Common errors in CI
If the chosen delimiter also appears in the text, sed ends the field early and reports sed: -e expression #1, char N: unknown option to s' or unterminated s' command. The fix is a delimiter that does not occur in either side of the substitution.