Search code examples
environment-variablesgit-bashwindows-terminal

How can I start Git Bash via Windows Terminal with environment variables set?


I am running Windows Terminal and Git Bash on my machine. I want to integrate Git Bash into Windows Terminal, but I want to set the environment variable MSYS_NO_PATHCONV=1 when running Git Bash via Windows Terminal.

I do not want to add this to my system environment variables, because I want to be able to start Git Bash outside of Windows Terminal without this environment variable.

To add Git Bash to Windows Terminal, I have created the following entry in Windows Terminal's profiles.json file:

        {
            "guid": "{3a20b825-3d22-41ed-b575-9006741aff64}",
            "name": "Git Bash",
            "commandline": "C:\\Program Files\\Git\\usr\\bin\\bash.exe",
            "icon": "C:/Program Files/Git/mingw64/share/git/git-for-windows.ico",
            "startingDirectory": "~",
            "hidden": false
        }

Here is what I have tried so far to get the environment variable to Git Bash:

  • Add this environment variable as a prefix in the command: MSYS_NO_PATHCONV=1 "commandline": "C:\\Program Files\\Git\\usr\\bin\\bash.exe",. This just results in an error message and does not start Git Bash.
  • Create a batch file which first sets the environment variable using the set command on Windows, then runs Git Bash. This doesn't open Git Bash and instead just opens a regular cmd terminal in which the commands from the batch file are executed.
  • Had a look around the documentation for the profiles.json file, but there is no mention of setting environment variable for terminal executables.

Is there any way that I can start my Git Bash in Windows Terminal with my custom environment variable without adding it to the system's environment variables?


Solution

  • "commandline": "cmd.exe /C \"set MSYS_NO_PATHCONV=1 && \"C:\\Program Files\\Git\\bin\\bash.exe\"\"",
    

    Or append export MSYS_NO_PATHCONV=1 to ~/.bashrc.