oscarvady oscarvady - 1 year ago 83
Bash Question

How to modify a config parameter value with slash

I am trying to modify a config file

from a
file. I can modify it using
, but the problem is when the value contains slash character.

For that I am using this code:

read -r -p "(Default PATH: /my/default/path/): " response
case $response in
(*[![:blank:]]*) sed -i '/key_one/s/= ''.*/= '$response'/' demo.cfg; echo 'OK';;
(*) echo 'Using default path'

Here the error when the variable
has slash:

sed: -e expression #1, char 20: unknown option to "s"

Could I scape this variable to use with sed function?


key_one = 1
key_two = 9

Thanks in advanced
Best regards

Answer Source

Try passing response like this


This replaces all / with \/. Example:

$ response=/my/path/to/target
$ echo ${response//\//\\/}

There is also an issue with your case statement. Your bash script should look like this:


read -r -p "(Default PATH: /my/default/path/): " response
    case $response in
        *[![:blank:]]*) sed -i "/key_one/s/= .*/= ${response//\//\\/}/" demo.cfg
                echo 'OK'
        *) echo 'Using default path'
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download