37

I've recently added a directory to the Windows PATH manually by going into Control Panel -> System -> Advanced system settings -> Environment Variables -> User variables -> PATH. (Windows 7, 64-bit.)

After rebooting and starting cmd.exe, echo %PATH% indicates that this worked: I see the directory I recently added in the output.

However, after starting Git Bash, the output of echo $PATH doesn't include this directory.

I could add export PATH=$PATH:/c/my/path in my bashrc but I'd rather Git Bash just get PATH from Windows so I don't have to remember to add paths to two places. How can this be accomplished?

(A more general related question is, what sets up Git Bash's $PATH? I see a couple of entries repeated in different places, some things that are in Windows %PATH% are in Git Bash's $PATH but not others. What all happens before I get the Git Bash prompt that touches $PATH?)

1
  • The path in question might be important: C:\cygwin\usr\x86_64-w64-mingw32\sys-root\mingw\bin. Jun 13, 2013 at 17:34

6 Answers 6

7

An mingw64 git bash session uses the executable /usr/bin/bash.exe, which doesn't access or modify the environment variable PATH (like it would in this unrelated vbs script, for instance)

A git bash session will simply add in front of your current PATH:

/mingw64/bin:/usr/bin:

It is possible that the mingW64 session packaged with msysgit won't consider a bin from another mingw installation: you can check it by setting another (simpler) directory to your PATH and see if it is still visible in your git bash session. If not, then it is a more general issue which concerns all directories that you would add to the PATH.

5
  • 3
    This doesn't seem to be correct. When I start git bash, its path is set up by some process that apparently converts the Windows PATH variable through some process. It is not as simple as adding extra items: ';' is translated to ':', drive specifiers are converted to directory names, and some other conversions are happening too. In some cases, this conversion is wrong -- "c:\Program Files\Java\jdk1.8.0_25"\bin in my windows path is converted to /c/Program Files/Java/jdk1.8.0_25"/bi in the git bash path (note missing first and last character) ... so the question is, how does this happen?
    – Jules
    Mar 23, 2018 at 10:27
  • 1
    @Jules That is possible indeed. A lot has changed in 5 years.
    – VonC
    Mar 23, 2018 at 10:39
  • The problem signalled by @Jules only seems to happen if the variable value (JAVA_HOME) is quoted (which normally is a good idea, but git bash somehow messes up). So if you make the value c:\Program Files\Java\jdk1.8.0_25 instead of "c:\Program Files\Java\jdk1.8.0_25", and then use %JAVA_HOME%\bin in your windows PATH this works OK for me.
    – Catweazle
    Oct 28, 2020 at 11:56
  • I don't think there's a /share directory any more, and I can't find a /usr/share/WinGit folder either. So even if these existed when the question was answered in 2013, they don't now.
    – AJM
    May 28, 2021 at 9:05
  • 1
    @AJM-Reinstate-Monica You are correct: I have updated those paths.
    – VonC
    May 28, 2021 at 9:20
7

Here's my small workaround for a similar problem (MSYS2 bash on Windows 10).

The idea is to convert the required paths to Unix-style paths and append them to bash $PATH, all done in .bashrc.

Do not append required paths to Win PATH. Instead create a new env var in Windows, like MSYS2_WINPATH, and append all the semicolon-separated Windows path directories to this variable. Append %MSYS2_WINPATH% to %PATH%.

Now insert this in your .bashrc -

################################## Construct PATH variable ##################################

winpath=$(echo $MSYS2_WINPATH | tr ";" "\n" | sed -e 's/\\/\\\\/g' | xargs -I {} cygpath -u {})
unixpath=''

# Set delimiter to new line
IFS=$'\n'

for pth in $winpath; do unixpath+=$(echo $pth)":"; done

export PATH=$(echo $PATH:$unixpath | sed -e 's/:$//g')
unset IFS
unset unixpath
unset winpath

################################# Constructed PATH variable #################################
2
  • Tried that in git-bash and it worked without the workaround for .bashrc. Thanks! Apr 10, 2018 at 6:47
  • good to hear that :) you're welcome.
    – anubhav
    Apr 10, 2018 at 14:52
2

If the PATH value would be too long after your user's PATH variable has been concatenated onto the environment PATH variable, Windows will silently fail to concatenate the user PATH variable.

This can easily happen after new software is installed and adds something to PATH, thereby breaking existing installed software. Windows fail!

The best fix is to edit one of the PATH variables in the Control Panel and remove entries you don't need. Then open a new CMD window and see if all entries are shown in "echo %PATH%".

2

I think that's what you wish you had.

That automaticly convert your Windows PATH to Bashrc:

https://github.com/hypolas/wintobashpath

https://github.com/hypolas/wintobashpath/releases/tag/v1.0.0

echo %PATH%
C:\Program Files (x86)\Common Files\Oracle\Java\javapath;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;C:\Program Files\dotnet\;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files\PowerShell\7\;C:\Program Files\nodejs;;E:\Program Files\Git\cmd;C:\Program Files\Go\bin;C:\Users\hypolas\AppData\Local\Microsoft\WindowsApps;C:\Users\hypolas\AppData\Local\Programs\Microsoft VS Code\bin;E:\msys64-2023\mingw64\bin;C:\Users\hypolas\AppData\Roaming\npm;C:\Users\hypolas\go\bin

Became:

/mingw64/bin:/usr/bin:/c/Users/hypolas/bin:/c/Users/hypolas/bin:/mingw64/bin:/usr/local/bin:/usr/bin:/usr/bin:/mingw64/bin:/usr/bin:/c/Users/hypolas/bin:/c/Program Files (x86)/Common Files/Oracle/Java/javapath:/c/Windows/system32:/c/Windows:/c/Windows/System32/Wbem:/c/Windows/System32/WindowsPowerShell/v1.0:/c/Windows/System32/OpenSSH:/c/Program Files/dotnet:/c/Program Files (x86)/NVIDIA Corporation/PhysX/Common:/c/Program Files/PowerShell/7:/c/Program Files/nodejs

I do that for myself.

1

Try moving the directory to the beginning of your path variable. I had the same issue as you after installing p4merge. The perforce directory was added to the path and p4merge was found by cmd.exe, but not git shell (mingw). After a fruitless search, I tried just editing the variable so that the perforce directory appeared first in my path. I fired up git shell and, voila, the directory is included in the output of $ echo $path, and $ p4merge opens up p4merge.

This is kind of a lame answer, since I'm clueless as to why it works, but if the work-around helps someone else, great.

0

Note that in git bash you can also extract the Windows user PATH from registry directly with:

cmd //c 'REG QUERY HKCU\Environment -v PATH'| grep '^    PATH'| sed 's/    PATH    REG_EXPAND_SZ    //'

For the system PATH, the key to use is HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment

Result can then be parsed (see @anubhav's answer) and be put in the PATH. The advantage is that this can be used to refresh your PATH during your session when Windows' PATH is updated.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .