Set a different shell for "git submodule foreach"

Viewed 93

It looks like Git uses /bin/sh as interpreter when executing script snippets through git submodule foreach '...'. Is there any way to change this, e.g. to convince git to use bin/bash instead?

I'm not looking for the obvious solution, i.e. to just enclose the script snippet into an extra bash -c '...', because that quickly ends up in quoting hell, because both git submodule foreach and bash -c should have the script snipped passed to them being enclosed in single quotes.

1 Answers

If I'm reading the code correctly, sh is hard coded in git's source code :

git submodule foreach ultimately calls run_command() from the run-command.c file, which goes through this instruction here when massaging its arguments :

    if (strcspn(argv[0], "|&;<>()$`\\\"' \t\n*?[#~=%") != strlen(argv[0])) {
#ifndef GIT_WINDOWS_NATIVE
        strvec_push(out, SHELL_PATH);
#else
        strvec_push(out, "sh");       // <- this instruction
#endif
        strvec_push(out, "-c");

        ...

(referenced code from version 2.29.0)


Since using another shell is as simple as writing your action in a script file and running it with said shell (either with a #!/bin/zsh line or with a zsh myscript.sh invocation), I suggest you resort to that if you need to run your actions with another shell.

Related