Re: Weird behavior of shell variables in git aliases
- From
Jeff King <peff@peff.net>
- Date
- Mar 21, 2011, 21:53 UTC
- Message-ID
- <20110321215310.GA2122@sigill.intra.peff.net>
- In-Reply-To
- <d9c38309-c433-476c-bba3-f2c5b7e94a89@k15g2000prk.googlegroups.com>
On Mon, Mar 21, 2011 at 09:39:43AM -0700, Dun Peal wrote:
Show 17 quoted lines
> It seems that a variable is available only once?! How can the > following shell session be explained: > > $ git config alias.test0 > !echo $1 > $ git test0 foo > foo > $ git config alias.test1 > !echo $1 && echo $1 > $ git test1 foo > > foo > $ git config alias.test2 > !BRANCH=$1 && echo $BRANCH && echo $BRANCH > $ git test2 foo > > foo
Because in v1.7.4 and earlier, we literally just tack the arguments (shell-quoted) onto the end of the string. So your alias ends up expanding to:
/bin/sh -c "!echo $1 && echo $1 'foo'"
So the first echo is empty, and then the second one echos foo. And what you are trying to do doesn't work with a straight alias (at the bottom I'll show you what you want).
But interestingly, that's _not_ the behavior as of Erik's 7f51f8b (alias: use run_command api to execute aliases, 2011-01-07), which is in master but not yet released. With that, we end up executing:
sh -c 'echo $1 && echo $1 "$@"' 'echo $1 && echo $1' 'foo'
which prints "foo foo". So it is technically a regression. I don't know how much we care; using positional parameters like this was already nonsensical, as shown above.
For reference, what you actually want (in either system) is:
$ git config alias.test1 !sh -c 'echo $1 && echo $1' - $ git test1 foo foo foo
Make sure to include the "-" (or some other string) which ends up as $0.
-Peff