threads / bug / 21540

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

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

## tl;dr

6 messages between Nov 8, 2009 and Nov 8, 2009.

replies: 5people: 4as markdown or json

Kate Ebneter· Nov 8, 2009, 22:11 UTC · lore
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

Sverre Rabbelier· Nov 8, 2009, 22:46 UTC · re: Kate Ebneter · lore

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

Heya
On Sun, Nov 8, 2009 at 23:11, Kate Ebneter <kate@ning.com> wrote:
> 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 :).

-- 
Cheers,

Sverre Rabbelier
Kate Ebneter· Nov 8, 2009, 22:50 UTC · re: Sverre Rabbelier · lore

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

On 11/8/09 2:46 PM, "Sverre Rabbelier" <srabbelier@gmail.com> wrote:
Show 16 quoted lines
> Heya
> 
> On Sun, Nov 8, 2009 at 23:11, Kate Ebneter <kate@ning.com> wrote:
>> 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 :).
*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

Björn Steinbrink· Nov 8, 2009, 22:49 UTC · re: Kate Ebneter · lore

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

On 2009.11.08 14:11:56 -0800, Kate Ebneter wrote:
Show 7 quoted lines
> 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

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
Jeff King· Nov 8, 2009, 23:03 UTC · re: Björn Steinbrink · lore

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

On Sun, Nov 08, 2009 at 11:49:55PM +0100, Björn Steinbrink wrote:
Show 13 quoted 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
>      ^^^
> 
> >     $ 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
Kate Ebneter· Nov 8, 2009, 23:10 UTC · re: Jeff King · lore

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

On 11/8/09 3:03 PM, "Jeff King" <peff@peff.net> wrote:
Show 23 quoted lines
> On Sun, Nov 08, 2009 at 11:49:55PM +0100, Björn Steinbrink wrote:
> 
>>> 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
>> 
>> 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.

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)

← back to recent threads