10

I'm using Git Bash v1.8.1, with a few aliases (for testing):

[alias]
    ekko = !echo $1 && echo $1
    ekko2 = !sh -c 'echo $1 && echo $1'

But when I run them, I see:

> git ekko master
master
master master

And:

> git ekko2 master
(blank line) 
(blank line) 

My intended behavior is:

> git ekko master
master
master

I'm fairly new to aliases - I'm looking for a way to ensure that my arguments are consumed entirely, and not appended to the end of the alias. Some sleuthing indicates this behavior changed somewhere around Git v1.7.x, and I haven't yet determined exactly how to accomplish this:

Git Alias - Multiple Commands and Parameters

Community
  • 1
  • 1
Craig Otis
  • 31,257
  • 32
  • 136
  • 234
  • Possible duplicate of [Git alias with positional parameters](https://stackoverflow.com/questions/3321492/git-alias-with-positional-parameters) – Michael Sep 03 '19 at 20:50

4 Answers4

16

Your ekko2 alias is really close... What you really want is this:

[alias]
    ekko2 = !sh -c 'echo $1 && echo $1' -

Git aliases that execute shell commands do substitute the $n variables, but they also append any arguments you pass them to the end of the command. So in your first example git ekko master is equivalent to echo master && echo master master, which explains its output.

Your second example is closer, but you're passing "master" to the sh command, which is just ignoring the extra argument. By adding - to the end of the alias, you're telling sh that the arguments that follow are intended for the script sh is executing, and not for sh itself.

Ajedi32
  • 45,670
  • 22
  • 127
  • 172
  • 4
    Just for the record, another relatively common way to achieve the same result is to use a function: `ekko2 = "!f() {echo $1 && echo $1}; f"`. It's potentially slightly more efficient, as it avoids forking a second shell, I think... – twalberg Aug 08 '13 at 15:09
  • 4
    For me, `ekko2 = "!echo $1; echo $2 #"` worked. The `#` suppresses the rest of the line. – donquixote Dec 29 '16 at 03:49
  • thanks @donquixote that '#' is making it all! don't know why I hustle so much to find this solution, it's much simpler than the !f() { ... }; f – Alan Apr 19 '23 at 13:07
5

ekko2 is missing a single dash at the end:

[alias]
    ekko2 = !sh -c 'echo $1 && echo $1' -

See the Git Wiki on Advanced aliases with arguments:

[alias]
    example = !sh -c 'ls $2 $1' -
4

You can add a Bash noop command ":" at the end:

git config --global alias.hello-world '!echo "$1"; :'

Prints:

$ git hello-world hello world
hello world
lumbric
  • 7,644
  • 7
  • 42
  • 53
3

On a Windows 10 machine using the standard command line the character # needs to be appended to the end of the alias and the whole alias wrapped in " characters. Eg.

[alias]
    ekko = "!echo $1 && echo $1 #"
Castrohenge
  • 8,525
  • 5
  • 39
  • 66
  • You saved my day!!! All other answers I saw dealt with linux or included some complicated unmaintainable stuff, this one is simple and reliable. – SebDieBln Dec 20 '22 at 11:10