【问题标题】:ZSH alias with parameter带参数的 ZSH 别名
【发布时间】:2016-03-24 06:35:40
【问题描述】:

我正在尝试为我的简单 git add/commit/push 创建一个带参数的别名。

我已经看到一个函数可以用作别名,所以我尝试了但没有成功。

在我有之前:

alias gitall="git add . ; git commit -m 'update' ; git push"

但我希望能够修改我的提交:

function gitall() {
    "git add ."
    if [$1 != ""]
        "git commit -m $1"
    else
        "git commit -m 'update'"
    fi
    "git push"
}

【问题讨论】:

    标签: shell zsh alias zshrc


    【解决方案1】:

    带参数的别名

    TL;DR:

    使用带参数的别名:

    alias foo='echo bar' 
    # works:
    foo 1
    # bar 1
    foo 1 2
    # bar 1 2
    

    展开

    (空格分隔)别名后的字符将按照您编写它们的顺序被视为参数。

    它们不能像函数一样被订购或改变。 例如。在函数或子shell的帮助下,确实可以通过别名将参数放入命令中间: 见@Tom's answer

    此行为类似于bash

    【讨论】:

      【解决方案2】:

      我尝试了接受的答案(Kevin's),但收到以下错误

      defining function based on alias `gitall'
      parse error near `()'
      
      

      因此,根据git issue 将语法更改为此,并且有效。

          function gitall {
          git add .
          if [ "$1" != "" ]
          then
              git commit -m "$1"
          else
              git commit -m update
          fi
          git push
          }
      

      【讨论】:

        【解决方案3】:

        你不能用参数创建别名*,它必须是一个函数。您的函数很接近,您只需要引用某些参数而不是整个命令,并在 [] 内添加空格。

        gitall() {
            git add .
            if [ "$1" != "" ] # or better, if [ -n "$1" ]
            then
                git commit -m "$1"
            else
                git commit -m update
            fi
            git push
        }
        

        *:大多数 shell 不允许在别名中使用参数,我相信 csh 和派生类可以,但 you shouldn't be using them anyway.

        【讨论】:

        • csh 有,但它根本没有功能。 (不知道是因为别名可以带参数而没有函数,还是因为没有函数而别名带参数,还是什么。)
        • 所以你会像gitall "my commit message"一样调用它(从shell)?还是你叫它gitall('my commit message')
        • @archae0pteryx 函数的调用方式与任何其他命令完全相同,因此 gitall "my commit message".
        • 我建议 getall() { 不使用前面的 function - 当然,在 zsh 中这两种方式都是合法的,但唯一的更改将使它与所有符合 POSIX 的 shell 兼容。
        • 顺便说一句,如果您使用git commit -m "${1:-update}"(提供默认值的参数扩展),那么您根本不需要if 语句。
        【解决方案4】:

        我在 .zshrc 文件中使用了这个函数:

        function gitall() {
            git add .
            if [ "$1" != "" ]
            then
                git commit -m "$1"
            else
                git commit -m update # default commit message is `update`
            fi # closing statement of if-else block
            git push origin HEAD
        }
        

        这里git push origin HEAD负责将你当前的分支推送到远程。

        从命令提示符运行此命令:gitall "commit message goes here"

        如果我们只运行gitall 而不使用任何提交消息,那么提交消息将是update,正如函数所说的那样。

        【讨论】:

        • 多么明确的答案!
        【解决方案5】:

        如果您出于某种原因确实需要使用带参数的别名,您可以通过在别名中嵌入一个函数并立即执行它来破解它:

        alias example='f() { echo Your arg was $1. };f'
        

        我看到这种方法在 .gitconfig 别名中使用了很多。

        【讨论】:

        • 如此骇人,却又如此美丽
        • 为什么要创建别名?只需调用函数example
        • 另外,迟来的,你需要在右括号前加一个分号。
        • 这很漂亮。有了这个,我可以创建一个别名,将别名添加到 rc 文件,然后重新加载所述 rc 文件。 ❤️ 别名 addalias='f() { echo "alias" $1 >> ~/.zshrc && . ~/.zshrc };f'
        • 不需要在全局范围内添加任何名称,只需使用匿名函数:alias example='(){ echo Your arg was $1. ;}'
        【解决方案6】:

        "git add ."" 之间的其他命令只是 bash 的字符串,删除 "s。

        您可能希望在 if 正文中使用 [ -n "$1" ]

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-01-11
          • 2016-10-21
          • 2011-06-11
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-12-26
          • 2020-07-19
          相关资源
          最近更新 更多