git/list[1] front-page[2] threads[3] people[4] search[5] about
 

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

From
Jeff King <peff@peff.net>
Date
Nov 8, 2009, 23:03 UTC
Message-ID
<20091108230300.GB17129@coredump.intra.peff.net>
In-Reply-To
<20091108224955.GA14095@atjola.homenet>
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
Previous: Björn SteinbrinkNext: Kate Ebneter
Message 5 of 6 in “Strange behavior (possible bug) using bash command subsitution with "git branch"”
  1. Kate EbneterNov 8, 2009
  2. Sverre RabbelierNov 8, 2009
  3. Kate EbneterNov 8, 2009
  4. Björn SteinbrinkNov 8, 2009
  5. Jeff KingNov 8, 2009
  6. Kate EbneterNov 8, 2009

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.