Skip to contentSkip to conversation

Strange behavior (possible bug) using bash command subsitution with "git branch"

DISCUSSION Kate Ebneter 4 participants 6 messages Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

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 master

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.

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

#1 lore raw
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 master

Your 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

#2 lore raw
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 master

Your 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

#3 lore raw
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
    * master
Quoted 8 lines
     ^^^
Quoted 3 lines
    $ branch=$(git branch)
    $ echo $branch
    gitosis.conf keydir master

Your 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)

#4 lore raw
In reply to Kate Ebneter

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
#5 lore raw
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