【问题标题】:bash script not working with multiple argumentsbash脚本不能使用多个参数
【发布时间】:2011-11-07 01:00:41
【问题描述】:

我正在尝试为 git commit 创建一个别名

function gcam() {
  git commit -a -m $@ ;
  git status
}

当我使用gcam 'something' 调用命令时,它可以正常工作,但是如果消息中间有空格,例如gcam 'new commit',则会出现消息Paths with -a does not make sense

我正在寻找this solution,但它对我不起作用,因为我使用的是$@ 而不是$1。为什么使用$@?只是如果我需要向 git commit 传递一个额外的参数。

有什么办法让它起作用吗?

提前致谢

【问题讨论】:

    标签: git bash command-line-arguments


    【解决方案1】:

    @Mat 说对了一半:你应该双引号 $@,然后也引用消息。 "$@" 扩展为参数列表,每个参数都作为一个单独的词(即好像每个参数都被单独引用):

    function gcam() {
      git commit -a -m "$@"
      git status
    }
    
    gcam "commit message" -v
    

    这相当于:

    git commit -a -m "commit message" -v
    git status
    

    【讨论】:

      猜你喜欢
      • 2022-10-23
      • 1970-01-01
      • 2023-03-04
      • 1970-01-01
      • 2011-09-30
      • 2022-10-23
      • 2021-11-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多