1

We can define git alias to run shell commands like this:

[alias]
    echo = !echo
    echo2 = !echo "$1" && echo "====" && echo "${@:2}" && :

(The last && : is used because the command line arguments are appended to the command again by git, and : turns them into no-op.)

Now my question is how does git run these commands. Does it spawn a shell (like sh) to run it?

I tried the above alias in two computers, one ubuntu and one centos. In ubuntu, the echo2 fails to expand the parameter ${@:2}, which in bash is expanded to the args starting from the second to the end of the list.

I guess that in ubuntu sh is used but sh is a link to dash. Unfortunately dash doesnot know ${@:2}. In centos sh is linked to bash and it works.

Can we have a way to choose the shell used in running these alias?

doraemon
  • 2,296
  • 1
  • 17
  • 36

1 Answers1

1

Now my question is how does git run these commands. Does it spawn a shell (like sh) to run it?

Yes. It runs /bin/sh. See https://stackoverflow.com/a/39445884/7976758

fails to expand the parameter ${@:2}

To run aliases with parameters there are 2 ways: use another level of shell or use shell functions. Examples:

[alias]
    echo3 = !bash -c 'echo "$0" && echo "====" && echo "${@:1}"'
    echo4 = !"f() { echo \"$1\" && echo \"====\" && echo \"${@:2}\"; }; f"

Alias echo4 declares a function and runs it. The last f runs the function.

phd
  • 82,685
  • 13
  • 120
  • 165
  • 1
    You can eliminate the need for the `bash` extension `${@:2}` by using `shift`: `echo "$1" && shift && echo "$@"`. – chepner Sep 20 '18 at 15:23