我正在尝试制作一个 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/