Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sed and replace string by a $variable [duplicate]

Tags:

bash

sed

I have a little problem.

I try to replace a string(well a line) by a $variable in a file.

So I use the command :

sed -i -e "s/conf .*/conf = $PATH_CONF/g" generals.conf

If PATH_CONF does not contains specials characters like "/", it's working.

But PATH_CONF contains a path(/home/etc.) so it has several "/", then I got an error :

 bad flag in substitute command: 'h' 

So how must I do to have specials characters in my $variable ?

thanks.

like image 593
Pompom Pidou Avatar asked Feb 21 '23 10:02

Pompom Pidou


1 Answers

Try:

sed -i -e "s@conf .*@conf = $PATH_CONF@g" generals.conf

You need the identical chars as separators (here @), not three /s necessarily.

like image 106
Mithrandir Avatar answered Feb 28 '23 21:02

Mithrandir