gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 09:09:09 24 4
gpt4 key购买 nike

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

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

我想运行此脚本将 file1.sh 的第一行替换为 working_path='$current_path';,但是 current_path/ 并且在 sed 命令中,/ 是在 sed 替换模式中预定义的。我试过这个:

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

上面一行,我想把变量current_path中的/替换成\/,然后输入current_path1 进入 sed 命令,但也有错误。

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

最佳答案

试试这个。

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

在替换命令中使用@ 代替/

关于bash - 在 bash 中如何替换字符串变量,并将其设置为 sed 命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19647982/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com