我正在尝试查看某个分支是否存在于 shell 脚本中。
然而,git-branch
似乎在插入时修改了它的输出。 (我不知道这里的确切现象或术语)
例如,我正在尝试获取分支数组:
$ git branch
develop
* master
$ branches=`git branch`
$ echo $branches
develop compiler.sh HOSTNAME index.html master
$ echo `git branch`
develop compiler.sh HOSTNAME index.html master
一种ls-files
似乎妨碍了。怎么来的?这是巴什吗? git ?我很困惑。
最佳答案
git branch
的输出包含 *
字符,表示您当前的分支:
$ git branch
develop
* master
在你的 shell 中运行 echo *
会打印出你的工作目录:
compiler.sh HOSTNAME index.html
所以你原来的问题出现了,因为在扩展之后,你实际上正在运行 echo develop * master
。
为避免这种目录通配行为,您可以在 echo
期间对 branches
进行强引号:
$ branches=`git branch`
$ echo "$branches"
develop
* master
关于git - 检查bash中是否存在git分支 : weird interpolation behavior,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13280077/