How do you pass a variable containing slashes as a pattern to sed
?
For example, if I have the following variable:
var="/Users/Documents/name/file"
I want to pass it to sed
as so:
sed "s/$var/replace/g" "$file"
However I get errors. How can I circumvent the issue?
Use an alternate regex delimiter as sed
allows you to use any delimiter (including control characters):
sed "s~$var~replace~g" $file