dougou6727 2019-02-15 15:15
浏览 870
已采纳

在Go中转义反引号(exec.Command中使用的字符串)

Given this code, I can't figure out how to escape the backtick.

var (
    MY_STRING = "something`something"
)
cmd := fmt.Sprintf("MY_ENV=%q;", MY_STRING)
out, err := exec.Command("bash", "-c", cmd).CombinedOutput()
// results in MY_ENV="something`something" ie unfinished input

I've tried the below but it results in "unknown escape sequence". It does work in the shell obviously. I've also tried to combine strings and raw string literals but with no success. How can I escape the backtick please?

var (
    MY_STRING = "something\`something"
)
  • 写回答

2条回答 默认 最新

  • dongmei8071 2019-02-15 15:28
    关注

    Use ' to escape ` in Bourne shells. And no need to quote the string.

    MY_STRING := "something'`'something"
    cmd := fmt.Sprintf("MY_ENV=%s;", MY_STRING)
    out, err := exec.Command("bash", "-c", cmd).CombinedOutput()
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 C++ /QT 内存权限的判断函数列举
  • ¥15 深度学习GFnet理解问题
  • ¥15 单细胞小提琴堆叠图代码
  • ¥80 LS dyna mpp并行报错
  • ¥50 升级strust2版本到2.3.15.1后使用ognl3.0.6.jar windows环境中没有问题,但部署到linux环境报错
  • ¥15 vue页面,node封装接口
  • ¥15 求TMS320F280039C工程模板!
  • ¥15 delphi+fastreport实现分组补空打印问题
  • ¥15 使用python把两台mysql数据库服务器数据导出和导入
  • ¥15 NodeBB论坛配置Apache Solr中文搜索引擎的详细教程