git - 为什么 git branch 和 $(git branch) 输出不同的结果

标签 git bash shell unix version-control

我正在尝试制作一个 bash 脚本,它将在其他分支中的当前分支之前打印提交。但是,当我在子 shell 中执行命令 git branch 时,我不仅获得了分支名称,还获得了当前目录中的文件和文件夹列表。为什么调用 $(git branch) 会这样?

最佳答案

这不是命令替换,而是引用。这是 git branch 在 repo 上的结果:

$ git branch
  5-job-test-fails
* master
  revisions
  with_cool_background

注意提问。

当您编写 echo $(git branch) 时,由于参数未被引号括起来,星号将扩展为当前目录中的文件:

$ echo $(git branch)
5-job-test-fails app bin cable config config.ru db erd.pdf Gemfile Gemfile.lock Guardfile lib log public Rakefile README.rdoc test tmp vendor master revisions with_cool_background

为了克服这个问题,引用论点:

$ echo "$(git branch)"
  5-job-test-fails
* master
  revisions
  with_cool_background

关于git - 为什么 git branch 和 $(git branch) 输出不同的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39555293/

相关文章:

linux - 比较 Bash 中按字母顺序排列的字符串、测试与双括号语法

bash - zsh 中的 checkwinsize 等效项?

arrays - Shell 中的数组操作

git - 如何通过 ssh 获取托管在 Azure DevOps Repo 中的 Terraform 模块

git bash 问题

Git 失败并显示 "Server aborted the SSL Handshake"

git - 我如何对分支上的所有提交进行加密签名?

bash - ansible wget 然后 exec scripts => get_url 等效

shell - hadoop在win10中,hadoop版本会导致找不到类org.apache.hadoop.util.VersionInfo,我该怎么办?

shell - vimdiff 与交互式 shell