-1

I don't understand the behavior of Bash in this case:

If I type git branch, I get a list of branches.

BUT if I try to put it into a variable, say, b=`git branch`; echo $b, I get an output as if I made ls; git branch. The content of a directory is listed together with the branches.

Could someone please explain this behavior?

1 Answers1

7

git branch indicates the current branch using *. When you run

b=`git branch`; echo $b

since $b is used unquoted with echo, this * is expanded by the shell, showing the files in the current directory. Quoting will avoid that:

b=`git branch`; echo "$b"
Kusalananda
  • 333,661
Stephen Kitt
  • 434,908