How to pass a variable containing slashes to sed command

Sometimes we need to search and replace variables containing slashes ('/') in Linux. The default format of sed command will not allow this as its using slashes ('/') as delimiter.

So we need to use an alternate regex delimiter as sed allows you to use any delimiter (including control characters).

Let's see one example.

#sed "s~$sourcevar~$replacevar~g" filename

Here we need to replace sourcevar, replacevar and filename accordingly.

This method will really helps in bash scripting.

That's all…

Leave a Reply

Your email address will not be published. Required fields are marked *