代码之家  ›  专栏  ›  技术社区  ›  sev

Bash alias命令的工作方式与我键入时不同

  •  0
  • sev  · 技术社区  · 4 年前

    我有一个bash别名来删除所有在远程上也被删除的git分支 Delete local Git branches after deleting them on the remote repo

    这是命令:

    alias gbpurge="git checkout main; git pull; git fetch --all -p; git branch -vv | grep gone | awk '{ print $1 }' | xargs -n 1 git branch -d"
    

    的输出 git branch -vv | grep gone 是:

      small-fixes                        dc454c7 [origin/small-fixes: gone] Removed superfluous tailwind class
      style-elements                     2103b56 [origin/style-elements: gone] Added design elements (background images etc.)
    

    当我将“”标记之间的内容复制粘贴到控制台时,此命令按预期工作并打印:

    error: The branch 'small-fixes' is not fully merged.
    If you are sure you want to delete it, run 'git branch -D small-fixes'.
    error: The branch 'style-elements' is not fully merged.
    If you are sure you want to delete it, run 'git branch -D style-elements'.
    

    (这些错误在这里是意料之中的,不是问题)

    但是,当我使用别名时,它会打印:

    error: The branch 'small-fixes' is not fully merged.
    If you are sure you want to delete it, run 'git branch -D small-fixes'.
    error: branch 'dc454c7' not found.
    error: branch '[origin/small-fixes:' not found.
    error: branch 'gone]' not found.
    error: branch 'Removed' not found.
    error: branch 'superflous' not found.
    error: branch 'tailwind' not found.
    error: branch 'class' not found.
    error: The branch 'style-elements' is not fully merged.
    If you are sure you want to delete it, run 'git branch -D style-elements'.
    error: branch '2103b56' not found.
    error: branch '[origin/style-elements:' not found.
    error: branch 'gone]' not found.
    error: branch 'Added' not found.
    error: branch 'design' not found.
    error: branch 'elements' not found.
    error: branch '(background' not found.
    error: branch 'images' not found.
    error: branch 'etc.)' not found.
    

    为什么它将提交消息单词作为别名中的分支名称转发?


    编辑-将外引号改为双引号,将1美元左右的内引号改为单引号

    0 回复  |  直到 4 年前
        1
  •  1
  •   sev    4 年前

    我没有正确地逃脱awk命令中的$符号。正确的别名是:

    alias gbpurge="git checkout main; git pull; git fetch --all -p; git branch -vv | grep gone | awk '{ print \$1 }' | xargs -n 1 git branch -d"
    
    
    推荐文章