Tuesday, January 30, 2024

[SOLVED] Set a different shell for "git submodule foreach"

Issue

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.


Solution

The path to the shell used is hard-coded when git is compiled.

git submodule foreach ultimately calls run_command() from the run-command.c file, which goes through these steps 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");
#endif
        strvec_push(out, "-c");

        ...

(code from version 2.43.0)

On Windows the value is always sh. Elsewhere, the value of (compile-time constant) SHELL_PATH is used, which is by default /bin/sh. You can check the value in your version of git with:

$ git var GIT_SHELL_PATH
/bin/sh

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.



Answered By - LeGEC
Answer Checked By - Timothy Miller (WPSolving Admin)