在bash中如何替换字符串变量,并将其设置为sed命令

min*_*ing 4 bash sed

在bash脚本文件中,我设置了一个这样的变量:

    current_path=`pwd`
    sed -i "1s/.*/working_path='$current_path';/" file1.sh
Run Code Online (Sandbox Code Playgroud)

我想运行这个脚本来替换file1.shinto 的第一行working_path='$current_path';,但是在命令中current_path/sed,它/是在sed替换模式中预定义的.我试过这个:

    current_path1="${current_path/\//\\\/}"
Run Code Online (Sandbox Code Playgroud)

上面这行,我想将/in变量替换current_path\/,然后输入current_path1sed命令中,但也有错误.

你能给我一些建议吗?谢谢.

Raf*_*afa 7

试试这个.

sed -i -e "1s@.*@working_path='$current_path';@" file1.sh
Run Code Online (Sandbox Code Playgroud)

使用@,而不是/在替换命令.