3

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?

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.