Git for Windows doesn't execute my .bashrc file
Asked Answered
C

4

62

I've just installed Git for Windows 2.5.0 on Windows 7, and it appears that my .bashrc file is not being executed when I run Git Bash.

I created the file like so:

Administrator@HintTech-Dev MINGW64 /
$ pwd
/

Administrator@HintTech-Dev MINGW64 /
$ cd ~

Administrator@HintTech-Dev MINGW64 ~
$ pwd
/c/Users/Administrator

Administrator@HintTech-Dev MINGW64 ~
$ touch .bashrc

Administrator@HintTech-Dev MINGW64 ~
$ vi .bashrc

[... I insert the line "ZZZTESTVAR=234" (without the quotes) into the file in vim ...]

Administrator@HintTech-Dev MINGW64 ~
$ exit

Yet, when I next run Git Bash:

Administrator@HintTech-Dev MINGW64 /
$ set | grep ZZZ

Administrator@HintTech-Dev MINGW64 /
$ cat ~/.bashrc
ZZZTESTVAR=234

Administrator@HintTech-Dev MINGW64 /
$ ZZZTESTVAR=234

Administrator@HintTech-Dev MINGW64 /
$ set | grep ZZZ
ZZZTESTVAR=234

Administrator@HintTech-Dev MINGW64 /
$

Why isn't my .bashrc being run? It seems to be in the right place and have the right permissions.

Cullender answered 24/8, 2015 at 16:6 Comment(2)
I am running Git bash version 1.9.5-preview20150319 and ran the exact same test you ran, but for me, it worked. My .bashrc was run. The variable was set. Do you have a .profile or .bash_profile in the way? These will stop .bashrc from being run.Heifetz
Nope, I don't even have a .profile or .bash_profile. But were you using Git for Windows 2.5.0?Cullender
C
106

OK, I found out the problem. Quite simply, the bash terminal used by the latest Git for Windows 2.5.0 (mintty) doesn't bother to read .bashrc - it reads .bash_profile. So you can set up your environment in .bash_profile and/or put this code at the start to read .bashrc:

if [ -f ~/.bashrc ]
then
    . ~/.bashrc
fi
Cullender answered 24/8, 2015 at 18:30 Comment(2)
It's not mintty per se but how git-bash.exe starts it. Long discussion about it here which basically ends with the installer being changed to create a .bash_profile like you describe: github.com/git-for-windows/git/issues/191Murial
Solution still works today May 2020 on windows 10 git bashKristalkristan
F
29

Same thing happened to me when I upgraded to Git Bash 2.5.0 in Windows 10. I renamed my '.bashrc' -> '.bash_profile' and relaunched Git Bash. Everything's working as usual again.

mv ~/.bashrc ~/.bash_profile
Flyaway answered 25/8, 2015 at 18:31 Comment(3)
That's almost identical to the one above, but they both work wellFoundling
be careful to not override your conda .bash_profileLarrabee
As at Oct 2022 once you have created .bashrc , .bash_profile is created automatically after reopening terminalCottontail
S
3

It appears the latest version of git for Windows (2.8.3.windows.1) uses a 'profile' file now instead of the .bash_profile. I assume this is so it isn't hidden and a valid file name. Didn't seem to cause any issues before, but maybe it was confusing to people.

Sadyesaechao answered 25/5, 2016 at 20:58 Comment(0)
A
1

A bit late on this answer perhaps, but you could call bash with the -rcfile argument followed by the location of your .bashrc file.

bash -rcfile C:\Users\name\.bashrc

I've added this to the end of my PowerShell profile (.ps1 file) so that Powershell automatically opens in bash and configured to my preferences.

Aspirant answered 5/3, 2019 at 19:25 Comment(2)
mintty has no param -rcfile, that's why .bash_profile is needed with minttyCeilidh
this worked for Terminal app that windows has: "C:\Program Files\Git\bin\bash.exe" -rcfile 'UserPathToYourEterntity/.bashrc'Turki

© 2022 - 2024 — McMap. All rights reserved.