背景:使用shell脚本,使用sed将字符/替换为\。
一直报错,搜了下资料,终于好了,记录下
错误1:
#!/bin/bash var="/media/sf_Project/self/smarty-frame/application/ApiLoveHouse/Controllers" var=${var#*application} string=$var | sed 's#\/#\\#g' echo $string
错误2:
#!/bin/bash var="/media/sf_Project/self/smarty-frame/application/ApiLoveHouse/Controllers" var=${var#*application} string=`$var | sed 's:\/:\\:g'` echo $string
错误3:
#!/bin/bash var="/media/sf_Project/self/smarty-frame/application/ApiLoveHouse/Controllers" var=${var#*application} string=`echo $var | sed 's:\/:\\:g'` echo $string
正确姿势:
var="/media/sf_Project/self/smarty-frame/application/ApiLoveHouse/Controllers" var=${var#*application} echo $var | sed 's:\/:\\:g' string=$(echo $var | sed 's#\/#\\#g') echo $string
返回
[root@vbox-nginx shell_command]# ./mkdemo.sh \ApiLoveHouse\Controllers \ApiLoveHouse\Controllers