2

When I use the command substitution operator in git-bash.exe, the output does not seem to be correct. For example:

$ git branch
  master
  one
* two

$ ls
a  b  c

$ echo $(git branch)
master one a b c two

As you can see, the output of $(git branch) seems to have the files in the current working directory inserted into it.

Why is this happening, and how can I fix it?

Joaquim d'Souza
  • 1,416
  • 2
  • 14
  • 25

1 Answers1

1

Note this isn't specific to git bash.

This is happening because the output of git branch has a "*" which tells you the branch you are on:

$ git branch
* master
  one
  two

The star is captured when the command is run

> cat file.sh
#!/usr/bin/env bash

set -x

BRANCHES=$(git branch)
> ./file.sh
++ git branch
+ BRANCHES='* master
  one
  two'

then expanded to the files in the working directory.

From this reference

> git branch --format='%(refname:short)'
master
one
two

you can strip out the star.

I believe we are advised against using the git porcelain commands for scripting though.

Akhil Nair
  • 3,144
  • 1
  • 17
  • 32