我正在尝试查看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
似乎正在阻碍。怎么会?这是Bash吗? Git的?我很困惑。
答案 0 :(得分:6)
git branch
的输出包含*
字符,表示您当前的分支:
$ git branch
develop
* master
在shell中运行echo *
将打印工作目录的全局:
compiler.sh HOSTNAME index.html
因此,您的原始问题就出现了,因为在扩展后,您实际上正在运行echo develop * master
。
要避免这种目录通配行为,您可以在branches
期间强引用echo
:
$ branches=`git branch`
$ echo "$branches"
develop
* master
答案 1 :(得分:3)
尝试这样做:
branches=$(git branch | sed 's/\(\*| \)//g')
我建议您使用sed
,因为*
字符是glob
的{{1}},因此它会扩展到所有文件和dir in当前目录。此外,我删除了不需要的额外空格。