如何转义这个 bash 脚本的字符串?

如何转义这个 bash 脚本的字符串?

这是我的foo.sh

#!/bin/bash

sed -i -- 's/<meta http-equiv="Content-Security-Policy" content="default-src * data: blob: 'unsafe-inline' 'unsafe-eval' ws: wss:;">/<meta my custom meta>/g' ../app-prod/ios/project/www/application/index.html

我不明白如何转义单引号和双引号以及星号符号。

答案1

您可能需要在 sed 周围使用双引号。然后在表达式中转义双引号和通配符。像这样的事情应该做:

sed -i "s|<meta http-equiv=\"Content-Security-Policy\" content=\"default-src \* data: blob: 'unsafe-inline' 'unsafe-eval' ws: wss:;\">|<meta my custom meta>|g" /path/to/file.html

(不确定双引号整个表达式。我似乎记得在单引号表达式中转义单引号在某些条件下可能会失败,......我无法命名)。

相关内容