Escape backslash in variable

别说谁变了你拦得住时间么 提交于 2019-12-02 05:24:29

问题


I am using sed to replace url's in a file, everything works fine just a hiccup when the url contains a '\'

exmaple url: http**://www.example.com/simi/icr

# variables
ICR_KEY=somekey
ICR_KEY_VAL="http\://www.example.com/simi/icr"
sed "s!${ICR_KEY}=.*!${ICR_KEY}=${ICR_KEY_VAL}!" properties > tmp

This replaces the URL, but the output does not contain the backslash from the variable value.


回答1:


Both bash and sed interpret the backslash as escape character. Use single quotes to prevent this for bash, and double the backslash for sed:

ICR_KEY_VAL='http\\://www.example.com/simi/icr'


来源:https://stackoverflow.com/questions/9487303/escape-backslash-in-variable

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!