7

I use MsysGit on windows 7. I have one annoying problem. The entire bash_profile file seems to get executed twice. for example, I have the following echo line in the .bash_profile

echo "Boinkk..."

enter image description here

If I have

echo "Calvin..."
echo "Hobbes..."

Then I get enter image description here So I know that the .bash_profile file is getting executed twice and not each statement getting executed twice. The target for the "Git Bash" Executable is

C:\Windows\System32\cmd.exe /c ""C:\Program Files\Software\Git\bin\sh.exe" --login -i"

Does anybody know what I have to do to get the bash shell to execute the bash_profile statements only once?

gdelfino
  • 147
Prasanth
  • 667

3 Answers3

6

I had the same problem and noticed there was no ~/.bashrc file.

Creating an empty ~/.bashrc resolved the issue:

touch ~/.bashrc

I could only speculate as to why this worked, but it did.

escapisam
  • 161
1

TL;DR — try removing --login from your bash invocation


If you're using Git for Windows with ConEmu or Cmder, the command to start bash probably looks something like this:

cmd /c ""%ConEmuDir%\..\git-for-windows\bin\bash" -i --login"

Note the --login bit. Apparently, if --login is passed to bash, it will first execute the commands from /etc/profile, then execute one of ~/.bash_profile, ~/.bash_login, or ~/.profile — whichever exists.

Now, msys provides an /etc/profile, which executes all scripts under /etc/profile.d. Cmder offers /etc/profile.d/cmder.sh, which executes ~/.bashrc (excerpt below)

# Source the users .bashrc file if it exists
if [ -f "${HOME}/.bashrc" ] ; then
    . "${HOME}/.bashrc"
fi

That's all done within the execution of /etc/profile. Afterward, bash --login will try to execute ~/.bash_profile. Git for Windows generates this ~/.bash_profile:

# generated by Git for Windows
test -f ~/.profile && . ~/.profile
test -f ~/.bashrc && . ~/.bashrc

Upon execution, ~/.bashrc gets run a second time.

Solution? Remove --login from bash's invocation. In Cmder/ConEmu, this can be done by hitting the down-arrow next to the plus button, finding your bash in the list, and changing the command to:

cmd /c ""%ConEmuDir%\..\git-for-windows\bin\bash" -i"

Without the --login bit, bash will skip executing /etc/profile, and only run ~/.bashrc (... and /etc/bash.bashrc, but msys doesn't execute the ~/.bashrc there)


theY4Kman
  • 111
1

I'm not familiar with how to fix on Windows but if it were UNIX/Linux you could do:

echo $PATH <br />

and see where you're getting your double entry from. I'm speculating that your .bash_profile is being added to the path more than once. If you track down where the path is being manipulated you can fix your problem.

C0D3M0NK3Y
  • 575
  • 3
  • 9