Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In bash how to replace string variable, and set it into sed command

Tags:

bash

sed

in a bash script file, I have set one variable like this:

    current_path=`pwd`
    sed -i "1s/.*/working_path='$current_path';/" file1.sh

I want to run this script to replace the first line of file1.sh into working_path='$current_path';, but the current_path has the / and in the sed command, the / is predefined in sed replace pattern. And I have tried this:

    current_path1="${current_path/\//\\\/}"

the above line, I want to replace the / in variable current_path into \/, then input the current_path1 into the sed command, but also has an error.

Could you give me some advice, please? Thanks.

like image 246
mining Avatar asked Jan 13 '23 01:01

mining


1 Answers

Try this.

sed -i -e "1s@.*@working_path='$current_path';@" file1.sh

Use @ instead of / in the substitute command.

like image 165
Rafa Avatar answered Jan 30 '23 00:01

Rafa