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

6 messages from 2009-11-08 to 2009-11-08. Participants: Kate Ebneter, Sverre Rabbelier, Björn Steinbrink, Jeff King.
Thread: https://gitlist.dev/t/21540

## Kate Ebneter, 2009-11-08 22:11

Subject: Strange behavior (possible bug) using bash command subsitution with "git branch"
Message-ID: <C71C822C.22429%kate@ning.com>
URL: https://gitlist.dev/e/C71C822C.22429%25kate%40ning.com

```
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, 2009-11-08 22:46

Subject: Re: Strange behavior (possible bug) using bash command subsitution with "git branch"
Message-ID: <fabb9a1e0911081446o7793eef1kcbad964c9a73edda@mail.gmail.com>
URL: https://gitlist.dev/e/fabb9a1e0911081446o7793eef1kcbad964c9a73edda%40mail.gmail.com
In-Reply-To: <C71C822C.22429%kate@ning.com>

```
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

```

## Björn Steinbrink, 2009-11-08 22:49

Subject: Re: Strange behavior (possible bug) using bash command subsitution with "git branch"
Message-ID: <20091108224955.GA14095@atjola.homenet>
URL: https://gitlist.dev/e/20091108224955.GA14095%40atjola.homenet
In-Reply-To: <C71C822C.22429%kate@ning.com>

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

```

## Kate Ebneter, 2009-11-08 22:50

Subject: Re: Strange behavior (possible bug) using bash command subsitution with "git branch"
Message-ID: <C71C8B2A.22436%kate@ning.com>
URL: https://gitlist.dev/e/C71C8B2A.22436%25kate%40ning.com
In-Reply-To: <fabb9a1e0911081446o7793eef1kcbad964c9a73edda@mail.gmail.com>

```
On 11/8/09 2:46 PM, "Sverre Rabbelier" <srabbelier@gmail.com> wrote:

> 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

```

## Jeff King, 2009-11-08 23:03

Subject: Re: Strange behavior (possible bug) using bash command subsitution with "git branch"
Message-ID: <20091108230300.GB17129@coredump.intra.peff.net>
URL: https://gitlist.dev/e/20091108230300.GB17129%40coredump.intra.peff.net
In-Reply-To: <20091108224955.GA14095@atjola.homenet>

```
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.

-Peff

```

## Kate Ebneter, 2009-11-08 23:10

Subject: Re: Strange behavior (possible bug) using bash command subsitution with "git branch"
Message-ID: <C71C8FD5.2243E%kate@ning.com>
URL: https://gitlist.dev/e/C71C8FD5.2243E%25kate%40ning.com
In-Reply-To: <20091108230300.GB17129@coredump.intra.peff.net>

```
On 11/8/09 3:03 PM, "Jeff King" <peff@peff.net> wrote:

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

```
