Friday, February 6, 2015

Linux sed Replace with Environment Variable Containing Path

I had a use case to construct a bash script to search a configuration file and replace a string with a path. This path was stored as an environment variable.

PATH=/some/path
export PATH

My original sed command looked like this:

sed -i "s/findThis/$PATH/g" someFile.cfg

Execution of the script kept throwing the following exception:

sed: -e expression #1, char 33: unknown option to `s'

After some trial and error, I discovered that the issue was related to the file separators (/) in the value of PATH. Instead of using a '/' as the separator for sed, use a pipe '|'.

sed -i "s|findThis|$PATH|g" someFile.cfg

No comments:

Post a Comment