Strange behavior (possible bug) using bash command subsitution with "git branch"
Hi, folks,
I ran into a weird situation while working on a script, which is best described with a little snippet from my gitosis-admin repository:
$ git branch
* master
$ branch=$(git branch)
$ echo $branch
gitosis.conf keydir masterThat is, if you run 'git branch' inside $() or ``, it lists the directories, too. I can't imagine that this is intended behavior, but perhaps I'm wrong.
I can reproduce this on CentOS, Ubuntu, Solaris, and Mac OS X using git 1.6.2.3, 1.6.3.2, 1.6.3.3, 1.6.4.2, and 1.6.5.2.
(In the end, I figured out a better way to write my script anyway, but this is just too weird not to ask about.)
Thanks,
Kate Ebneter
Somewhat befuddled build engineer
Quoted 7 lines · Kate Ebneter
Hi, folks,
I ran into a weird situation while working on a script, which is best described with a little snippet from my gitosis-admin repository:
$ git branch * master
^^^
Quoted 3 lines
$ branch=$(git branch)
$ echo $branch
gitosis.conf keydir masterYour shell expands the *, thus echo sees all the things in the current directory. Use 'echo "$branch"' and you'll see what you expected.
Björn
Quoted 5 lines · Björn Steinbrink
Quoted 5 lines
I ran into a weird situation while working on a script, which is best described with a little snippet from my gitosis-admin repository:
$ git branch * master
Quoted 8 lines
^^^Quoted 3 lines
$ branch=$(git branch) $ echo $branch gitosis.conf keydir masterYour shell expands the *, thus echo sees all the things in the current directory. Use 'echo "$branch"' and you'll see what you expected.
And more to the point, this is just one reason why one should use for-each-ref when scripting (the other is that git-branch's output is considered porcelain, and is not guaranteed to remain stable). E.g.:
git for-each-ref --format='%(refname:short)' refs/heads/
would produce the output the original poster wanted.
-Peff
Quoted 16 lines · Jeff King
Quoted 5 lines · Björn Steinbrink
Quoted 5 lines
I ran into a weird situation while working on a script, which is best described with a little snippet from my gitosis-admin repository:
$ git branch * masterQuoted 8 lines
^^^Quoted 3 lines
$ branch=$(git branch) $ echo $branch gitosis.conf keydir masterYour shell expands the *, thus echo sees all the things in the current directory. Use 'echo "$branch"' and you'll see what you expected.
Quoted 7 lines
And more to the point, this is just one reason why one should use for-each-ref when scripting (the other is that git-branch's output is considered porcelain, and is not guaranteed to remain stable). E.g.:
git for-each-ref --format='%(refname:short)' refs/heads/would produce the output the original poster wanted.
Ah, sweet, I did not know about that. I ended up doing something entirely different (short form: my original idea about what I wanted to do was wrong) but that's very helpful for the future.
Thanks,
Kate Ebneter (who is gradually learning more and more about git)
Heya
Quoted 2 lines · Kate Ebneter
That is, if you run 'git branch' inside $() or ``, it lists the directories, too. I can't imagine that this is intended behavior, but perhaps I'm wrong.
Try enclosing it in quotes ;).
That is, compare:
$ echo "`git branch`" with $ echo `git branch`
The kicker is that there's a * in the output of git branch, which your shell helpfully substitutes with the contents of your current directory :).
Signature · 3 lines
Cheers, Sverre Rabbelier
Quoted 8 lines · Sverre Rabbelier
Heya
Quoted 2 lines · Kate Ebneter
That is, if you run 'git branch' inside $() or ``, it lists the directories, too. I can't imagine that this is intended behavior, but perhaps I'm wrong.
Try enclosing it in quotes ;).
Quoted 8 lines
That is, compare:
$ echo "`git branch`" with $ echo `git branch`The kicker is that there's a * in the output of git branch, which your shell helpfully substitutes with the contents of your current directory :).
*head desk*
You know, I spent hours last night trying to figure that out. So simple. Duh.
Thanks for pointing that out -- I knew it had to be something simple, but sometimes you just can't see the forest for the trees! :-)
Thanks again,
Kate Ebneter