Reputation: 23502
I'm trying to make a bash script which will print commits ahead of current branch in other branches. However, when I execute command git branch
in a subshell, I get not only branch names but also list of files and folders in the current directory. Why calling $(git branch)
behaves this way?
Upvotes: 3
Views: 418
Reputation: 33327
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
Upvotes: 6