我正在尝试查看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?我很困惑。
发布于 2012-11-08 07:44:15
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发布于 2012-11-08 07:30:07
试着这样做:
branches=$(git branch | sed 's/\(\*| \)//g')我建议您使用sed,因为*字符是shell的glob,所以它被扩展到当前目录中的所有文件和目录。此外,我删除了不需要的额外空格。
https://stackoverflow.com/questions/13280077
复制相似问题