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

in the bash script file, I set one variable as follows:

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

I want to run this script, to replace the first line file1.shon working_path='$current_path';, but current_pathis /part of a team sed, /predestined to sedreplace the template. And I tried this:

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

the above line, I want to replace /the variable current_pathwith \/, then enter current_path1the command sed, but also has an error.

Could you give me some advice please? Thanks.

+4
source share
3 answers

Try it.

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

@ / .

+7

s///:

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

, :

current_path=`pwd`
sed -i -e "1i\working_path='$current_path)';" -e 1d file1.sh

.sh? "she-bang"?

+2

Please add '/' to the beginning of the pattern line. It replaces all pattern matches with a string.

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

All Articles