2

I am trying to create a git alias that takes a string as a entire positional paramter.

For e.g. I have a alias as follows:

my-alias = "!f() { \
     param1 = $1; \
     param2 = $2; \
     rem_param = ${@:3};\
     };f"

And I want to use alias as:

$ git my-alias abc "this is one parameter" 11 22 44

Here my parameters should be

param1 = abc
param2 = "this is one parameter"
rem_pram = 11 22 44

The way I have setup it starts treating every word in the string as a separate parameter. So the question is there any way to treat the string as one parameter?

patronus
  • 103
  • 1
  • 7

1 Answers1

4

When you start a Git alias with !, that tells Git to invoke the shell for your alias. By default, the shell expands words with whitespace, and so your assignment to param2 consists of only "this". To solve this, you need to double-quote your arguments. Since you already have double quotes here, you need to escape them. Furthermore, to have syntactically valid assignments, you also need to remove the spaces around the equals sign.

So things would look like this:

my-alias = "!f() { \
    param1=\"$1\"; \
    param2=\"$2\"; \
    rem_param=\"${@:3}\"; \
};f"

I should also point out that because Git always uses /bin/sh as the shell for aliases (unless it was compiled explicitly for a different one) the ${@:3} syntax will only work on systems that use bash as /bin/sh. This alias fails on most Debian and Ubuntu systems, where /bin/sh is dash.

bk2204
  • 64,793
  • 6
  • 84
  • 100
  • I am using using git-bash for windows and adding the quotes still doesn't fix the issue :( When I print param2 it still shows up as "this". And yes I don't have spaces around equals sign in my .gitconfig file. – patronus Nov 01 '18 at 17:40
  • Sorry I understood what the problem was. The issue is fixed. – patronus Nov 01 '18 at 22:23