3

What default shell is used by git when executing an alias starting with !?

I'm suspecting it's /bin/sh, because I'm getting different behaviour when using read in an alias vs in my shell (which is bash).

+ravi@boxy:~/.dotfiles(master*% u+2)$ grep 'test =' ~/.gitconfig    
        test = "!f() { read -n1 -p "Works?" r; } ; f "
+ravi@boxy:~/.dotfiles(master*% u+2)$ git test
f() { read -n1 -p Works? r; } ; f : 1: read: Illegal option -n
+ravi@boxy:~/.dotfiles(master*% u+2)[2]$ read -n1 -p "Works?" r; # Try it in current shell (bash)
Works?Y+ravi@boxy:~/.dotfiles(master*% u+2)$ # OK, that worked...
+ravi@boxy:~/.dotfiles(master*% u+2)$ grep 'sh =' ~/.gitconfig
        sh = "!f() { exec \"$@\"; }; f"
+ravi@boxy:~/.dotfiles(master*% u+2)$ git sh env | grep SHELL
SHELL=/bin/bash
+ravi@boxy:~/.dotfiles(master*% u+2)$ # WTF??????????
+ravi@boxy:~/.dotfiles(master*% u+2)$ type read
read is a shell builtin
+ravi@boxy:~/.dotfiles(master*% u+2)$ which read
+ravi@boxy:~/.dotfiles(master*% u+2)[1]$ # none, so why is the shell behaving differently?
+ravi@boxy:~/.dotfiles(master*% u+2)[1]$ git sh bash -c read -n1
Hmm, this is ok??
+ravi@boxy:~/.dotfiles(master*% u+2)$ # Yes, that worked.

How can I set which shell is used in processsing aliases beginning with !?

Tom Hale
  • 40,825
  • 36
  • 187
  • 242

1 Answers1

4

As described here, you can see more with:

GIT_TRACE=1 git test

The workaround is to create a shell script (whose first line can be a shebang #!/bin/bash) named git-xxx. Then any git xxx call would invoke that script (provided it is in the $PATH).

As the OP Tom Hale adds in the comments, /bin/sh is used:

trace: exec: '/bin/sh' '-c' 'echo Which shell?' 
'echo Which shell?'
Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • `trace: exec: '/bin/sh' '-c' 'echo Which shell?' 'echo Which shell?'` – Tom Hale Sep 13 '16 at 09:40
  • @TomHale Thank you. I have included your comment in the answer for more visibility. – VonC Sep 13 '16 at 11:25
  • 1
    Especially like the reference to how to create git custom commands. I had trouble with the shell as well. I tried making it commands in the path and it works like a charm. Thumbs up! – Adrien Oct 28 '19 at 11:38