csimp
csimp

Reputation: 3

Bash replace line using sed and 2 variables

I am trying to replace a line in a file using sed and 2 variables. I tried this:

    varFind=$(echo "cfg_dir=/usr/local/.file1.cfg")
    varReplace=$(echo "cfg_dir=/usr/dir1/file2.cfg")

    sed -i "s/${varFind}/${varReplace}/" /usr/local/file.txt

But it keeps throwing this:

    user@localhost:~# ./script.sh sed: -e expression #1, char 17: unknown option to `s'

What am I doing wrong? I've looked and it appears to be right to me.

Upvotes: 0

Views: 1658

Answers (1)

Sleafar
Sleafar

Reputation: 1526

The simplest way to fix your problem is to change the separator for the s command. Change your sed command to this:

sed -i "s%${varFind}%${varReplace}%" /usr/local/file.txt

This works as long as your variables don't contain %.

Upvotes: 2

Related Questions