简体   繁体   English

Shell 脚本用变量 linux 替换字符串

[英]Shell Scripting replace string with variable linux

I am trying to replace string like "c:\abacd" with a variable containing ex.我正在尝试用包含 ex 的变量替换像“c:\abacd”这样的字符串。 v_var="e:\abcd" in shell scripting. shell 脚本中的 v_var="e:\abcd"。 but below code is not working.但下面的代码不起作用。

sed -i "s+c:\abcd+${v_var}+g" ~/test.txt sed -i "s+c:\abcd+${v_var}+g" ~/test.txt

but, i am successfully able to replace other simple words like "test" with variable containing v_rest="rest" in a file using cmd below.但是,我可以使用下面的 cmd 在文件中成功地用包含 v_rest="rest" 的变量替换其他简单的单词,例如“test”。 i am using variable我正在使用变量

sed -i "s+test+${v_rest}+g" ~/test.txt sed -i "s+test+${v_rest}+g" ~/test.txt

This worked for me:这对我有用:

export v_var="e:\\\\abcd"
sed -i "s/c:\\\\abcd/${v_var}/g" ~/test.txt

You need to escape the '\' in the command-line and variable.您需要在命令行和变量中转义“\”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM