【问题标题】:How do I prevent bash from escaping backslash in my npm script? [duplicate]如何防止 bash 在我的 npm 脚本中转义反斜杠? [复制]
【发布时间】:2019-01-21 05:42:22
【问题描述】:

我的 npm 脚本需要运行 sed 来替换一些字符串,例如

"prebuild":"sed 's/http:\/\/example.com/https:\/\/something_real.com/' < static/config.js.example > static/config.js",

"build": "node build/build.js"

但是运行npm run build时shell会解释反溅转义,所以我得到了错误,

qiulang$ npm run build
> sed 's/http://example.com/https://something_real/' < static/config.js.example > static/config.js

sed: 1: "s/http://emicall-cmb.em ...": bad flag in substitute command: 'e'

那么如何防止 shell 这样做呢?

【问题讨论】:

  • 我认为为什么 \/ 在这里不起作用可能是因为它在双引号内,因此您需要 \\/ ,使用不同的分隔符更好..另外,您需要转义 @ 987654326@ 在替换命令的搜索部分,因为它是一个元字符

标签: node.js bash shell npm sed


【解决方案1】:

您可以使用不同的字符作为分隔符

sed 's@http://example.com@https://something_real.com@' < static/config.js.example > static/config.js

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-10-30
    • 1970-01-01
    • 1970-01-01
    • 2019-09-28
    • 1970-01-01
    • 2015-09-22
    • 1970-01-01
    • 2016-10-28
    相关资源
    最近更新 更多