why git branch and $(git branch) outputs different result why git branch and $(git branch) outputs different result shell shell

why git branch and $(git branch) outputs different result


It's not the command substitution, it's the quoting. Here is the result of git branch on a repo:

$ git branch  5-job-test-fails* master  revisions  with_cool_background

Notice the askerisk.

When you write echo $(git branch), since the argument is unquoted, the asterisk will expand to the files in the current directory:

$ echo $(git branch)5-job-test-fails app bin cable config config.ru db erd.pdf Gemfile Gemfile.lock Guardfile lib log public Rakefile README.rdoc test tmp vendor master revisions with_cool_background

To overcome this, quote the argument:

$ echo "$(git branch)"  5-job-test-fails* master  revisions  with_cool_background