【问题标题】:What do double curly braces do in a bash command?双花括号在 bash 命令中的作用是什么?
【发布时间】:2020-04-09 11:02:21
【问题描述】:

我正在重构一个脚本,我看到了这个:

/bin/bash -c 'rm -rf directory/{{..?*,.[!.]*,*}}'

到目前为止,奇怪的是,谷歌搜索“bash 双花括号”并没有让我有所收获。这段代码有什么作用?

编辑:显然,我没有提供足够的细节。它实际上是一个使用format() 的python 命令,因此双花括号实际上是为此转义的单花括号。

subprocess.check_output(['/bin/bash', '-c', 'rm -rf {0}/{{..?*,.[!.]*,*}}'.format('directory')]

@thatotherguy 把它钉在了 cmets 上。

【问题讨论】:

  • 你要重构什么样的脚本?您确定 {{ 没有用于外部脚本中的某种模板或转义吗?
  • @thatotherguy 嗯,这是一个使用subprocess.check_output() 运行命令的python 脚本,但没有进行模板化。我显示的就是执行的。
  • 这看起来像是bash -c 'rm -rf directory/{..?*,.[!.]*,*}' 的拼写错误,除非directory 实际上确实包含名称以{ 开头并以} 结尾的文件/目录 - 试试echo directory/{{..?*,.[!.]*,*}}echo directory/{..?*,.[!.]*,*}
  • @mattalxndr 您介意发布更多上下文且不加删节吗?我猜真正的代码类似于subprocess.check_output("/bin/bash -c '{}/{{..?*,.[!.]*,*}}'".format("mydirectory")) 可以解释它
  • @thatotherguy 你是对的。如果您将其添加为答案,我可以接受。

标签: python bash command escaping curly-braces


【解决方案1】:

例如,在 GitLab 或 GitHub 运行器中传递 bash 脚本时,有时会出现双花卷

  - name: Set selected color
    run: echo '::set-output name=SELECTED_COLOR::green'
    id: random-color-generator
  - name: Get color
    run: echo "The selected color is ${{ steps.random-color-generator.outputs.SELECTED_COLOR }}"

https://docs.github.com/en/actions/reference/workflow-commands-for-github-actions

【讨论】:

    猜你喜欢
    • 2014-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-30
    • 2020-03-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多