{"thread":{"id":"21540","subject":"Strange behavior (possible bug) using bash command subsitution with \"git branch\"","startedAt":"2009-11-08T22:11:56Z","lastAt":"2009-11-08T23:10:13Z","messageCount":6,"participants":["Kate Ebneter","Sverre Rabbelier","Björn Steinbrink","Jeff King"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"127101","messageId":"C71C822C.22429%kate@ning.com","threadId":"21540","inReplyTo":null,"subject":"Strange behavior (possible bug) using bash command subsitution with \"git branch\"","fromName":"Kate Ebneter","fromEmail":"kate@ning.com","sentAt":"2009-11-08T22:11:56Z","receivedAt":"2009-11-08T22:11:56Z","isPatch":false,"sender":{"key":"kate@ning.com","avatar":null},"body":"Hi, folks,\n\nI ran into a weird situation while working on a script, which is best\ndescribed with a little snippet from my gitosis-admin repository:\n\n    $ git branch\n    * master\n    $ branch=$(git branch)\n    $ echo $branch\n    gitosis.conf keydir master\n\n\nThat is, if you run 'git branch' inside $() or ``, it lists the directories,\ntoo. I can't imagine that this is intended behavior, but perhaps I'm wrong.\n\nI can reproduce this on CentOS, Ubuntu, Solaris, and Mac OS X using git\n1.6.2.3, 1.6.3.2, 1.6.3.3, 1.6.4.2, and 1.6.5.2.\n\n(In the end, I figured out a better way to write my script anyway, but this\nis just too weird not to ask about.)\n\nThanks,\nKate Ebneter\nSomewhat befuddled build engineer\n"},{"id":"127102","messageId":"fabb9a1e0911081446o7793eef1kcbad964c9a73edda@mail.gmail.com","threadId":"21540","inReplyTo":"C71C822C.22429%kate@ning.com","subject":"Re: Strange behavior (possible bug) using bash command subsitution with \"git branch\"","fromName":"Sverre Rabbelier","fromEmail":"srabbelier@gmail.com","sentAt":"2009-11-08T22:46:33Z","receivedAt":"2009-11-08T22:46:33Z","isPatch":false,"sender":{"key":"srabbelier@gmail.com","avatar":"https://avatars.githubusercontent.com/u/3098?v=4"},"body":"Heya\n\nOn Sun, Nov 8, 2009 at 23:11, Kate Ebneter <kate@ning.com> wrote:\n> That is, if you run 'git branch' inside $() or ``, it lists the directories,\n> too. I can't imagine that this is intended behavior, but perhaps I'm wrong.\n\nTry enclosing it in quotes ;).\n\nThat is, compare:\n$ echo \"`git branch`\"\nwith\n$ echo `git branch`\n\nThe kicker is that there's a * in the output of git branch, which your\nshell helpfully substitutes with the contents of your current\ndirectory :).\n\n-- \nCheers,\n\nSverre Rabbelier\n"},{"id":"127103","messageId":"20091108224955.GA14095@atjola.homenet","threadId":"21540","inReplyTo":"C71C822C.22429%kate@ning.com","subject":"Re: Strange behavior (possible bug) using bash command subsitution with \"git branch\"","fromName":"Björn Steinbrink","fromEmail":"b.steinbrink@gmx.de","sentAt":"2009-11-08T22:49:55Z","receivedAt":"2009-11-08T22:49:55Z","isPatch":false,"sender":{"key":"b.steinbrink@gmx.de","avatar":"https://avatars.githubusercontent.com/u/230962?v=4"},"body":"On 2009.11.08 14:11:56 -0800, Kate Ebneter wrote:\n> Hi, folks,\n> \n> I ran into a weird situation while working on a script, which is best\n> described with a little snippet from my gitosis-admin repository:\n> \n>     $ git branch\n>     * master\n     ^^^\n\n>     $ branch=$(git branch)\n>     $ echo $branch\n>     gitosis.conf keydir master\n\nYour shell expands the *, thus echo sees all the things in the current\ndirectory. Use 'echo \"$branch\"' and you'll see what you expected.\n\nBjörn\n"},{"id":"127104","messageId":"C71C8B2A.22436%kate@ning.com","threadId":"21540","inReplyTo":"fabb9a1e0911081446o7793eef1kcbad964c9a73edda@mail.gmail.com","subject":"Re: Strange behavior (possible bug) using bash command subsitution with \"git branch\"","fromName":"Kate Ebneter","fromEmail":"kate@ning.com","sentAt":"2009-11-08T22:50:18Z","receivedAt":"2009-11-08T22:50:18Z","isPatch":false,"sender":{"key":"kate@ning.com","avatar":null},"body":"On 11/8/09 2:46 PM, \"Sverre Rabbelier\" <srabbelier@gmail.com> wrote:\n\n> Heya\n> \n> On Sun, Nov 8, 2009 at 23:11, Kate Ebneter <kate@ning.com> wrote:\n>> That is, if you run 'git branch' inside $() or ``, it lists the directories,\n>> too. I can't imagine that this is intended behavior, but perhaps I'm wrong.\n> \n> Try enclosing it in quotes ;).\n> \n> That is, compare:\n> $ echo \"`git branch`\"\n> with\n> $ echo `git branch`\n> \n> The kicker is that there's a * in the output of git branch, which your\n> shell helpfully substitutes with the contents of your current\n> directory :).\n\n*head desk*\n\nYou know, I spent hours last night trying to figure that out. So simple.\nDuh.\n\nThanks for pointing that out -- I knew it had to be something simple, but\nsometimes you just can't see the forest for the trees! :-)\n\nThanks again,\nKate Ebneter\n"},{"id":"127105","messageId":"20091108230300.GB17129@coredump.intra.peff.net","threadId":"21540","inReplyTo":"20091108224955.GA14095@atjola.homenet","subject":"Re: Strange behavior (possible bug) using bash command subsitution with \"git branch\"","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2009-11-08T23:03:00Z","receivedAt":"2009-11-08T23:03:00Z","isPatch":false,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"On Sun, Nov 08, 2009 at 11:49:55PM +0100, Björn Steinbrink wrote:\n\n> > I ran into a weird situation while working on a script, which is best\n> > described with a little snippet from my gitosis-admin repository:\n> > \n> >     $ git branch\n> >     * master\n>      ^^^\n> \n> >     $ branch=$(git branch)\n> >     $ echo $branch\n> >     gitosis.conf keydir master\n> \n> Your shell expands the *, thus echo sees all the things in the current\n> directory. Use 'echo \"$branch\"' and you'll see what you expected.\n\nAnd more to the point, this is just one reason why one should use\nfor-each-ref when scripting (the other is that git-branch's output is\nconsidered porcelain, and is not guaranteed to remain stable). E.g.:\n\n  git for-each-ref --format='%(refname:short)' refs/heads/\n\nwould produce the output the original poster wanted.\n\n-Peff\n"},{"id":"127109","messageId":"C71C8FD5.2243E%kate@ning.com","threadId":"21540","inReplyTo":"20091108230300.GB17129@coredump.intra.peff.net","subject":"Re: Strange behavior (possible bug) using bash command subsitution with \"git branch\"","fromName":"Kate Ebneter","fromEmail":"kate@ning.com","sentAt":"2009-11-08T23:10:13Z","receivedAt":"2009-11-08T23:10:13Z","isPatch":false,"sender":{"key":"kate@ning.com","avatar":null},"body":"On 11/8/09 3:03 PM, \"Jeff King\" <peff@peff.net> wrote:\n\n> On Sun, Nov 08, 2009 at 11:49:55PM +0100, Björn Steinbrink wrote:\n> \n>>> I ran into a weird situation while working on a script, which is best\n>>> described with a little snippet from my gitosis-admin repository:\n>>> \n>>>     $ git branch\n>>>     * master\n>>      ^^^\n>> \n>>>     $ branch=$(git branch)\n>>>     $ echo $branch\n>>>     gitosis.conf keydir master\n>> \n>> Your shell expands the *, thus echo sees all the things in the current\n>> directory. Use 'echo \"$branch\"' and you'll see what you expected.\n> \n> And more to the point, this is just one reason why one should use\n> for-each-ref when scripting (the other is that git-branch's output is\n> considered porcelain, and is not guaranteed to remain stable). E.g.:\n> \n>   git for-each-ref --format='%(refname:short)' refs/heads/\n> \n> would produce the output the original poster wanted.\n\nAh, sweet, I did not know about that. I ended up doing something entirely\ndifferent (short form: my original idea about what I wanted to do was wrong)\nbut that's very helpful for the future.\n\nThanks,\nKate Ebneter (who is gradually learning more and more about git)\n"}]}