Cygwin .bashrc not being source because cygwin starting in Windows home

NOTE: Please see my second edit below for an update on the problem.

Cygwin was working fine for me until last week. Now .bashrc isn't getting sourced. I must have installed something or changed something but I can't remember exactly what caused the problem. When I start Cygwin, I'm in my Windows home and none of my aliases work. I have to manually source .bashrc. The following is my Cygwin.bat:

@echo off

C:
chdir C:\cygwin\bin

set CYGWIN=tty notitle glob

bash --login -i

Any ideas?

EDIT: My .bash_profile contains the following,

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

EDIT2: IMPORTANT! When I started Cygwin it starts in my Windows home folder. I tried putting my .bash_profile and .bashrc in this folder and they were sourced correctly! This means that the problem has been reduced to figuring out why Cygwin starts in the Windows home folder and not the normal Cygwin home folder.

EDIT3: Results of running grep Gulshan /etc/passwd/

$ grep Gulshan /etc/passwd
Administrator:unused:500:513:U-Gulshan-HP\Administrator,S-1-5-21-1235613160-4193452482-2032876723-500:/home/Administrator:/bin/bash
Guest:unused:501:513:U-Gulshan-HP\Guest,S-1-5-21-1235613160-4193452482-2032876723-501:/home/Guest:/bin/bash
Gulshan:unused:1000:513:U-Gulshan-HP\Gulshan,S-1-5-21-1235613160-4193452482-2032876723-1000:/home/Gulshan:/bin/bash

Solution 1:

The Cygwin FAQ explains how $HOME is set:

When starting Cygwin from Windows, HOME is determined as follows in order of decreasing priority:

  • HOME from the Windows environment, translated to POSIX form.
  • The entry in /etc/passwd
  • /home/USERNAME

When using Cygwin from the network (telnet, ssh,...), HOME is set from /etc/passwd.

If your HOME is set to a value such as /cygdrive/c, it is likely that it was set in Windows. Start a DOS Command Window and type set HOME to verify if this is the case.

Access to shared drives is often restricted when starting from the network, thus Domain users may wish to have a different HOME in the Windows environment (on shared drive) than in /etc/passwd (on local drive). Note that ssh only considers /etc/passwd, disregarding HOME.

My best guess is that you've messed up your /etc/passwd file. (The Windows home directory is %USERPROFILE%, not %HOME%.)

What does grep USERNAME /etc/passwd print (replacing USERNAME with your user name)?

Solution 2:

If you have a .profile or .bash_profile, that is sourced on login shells, and you get a login shell since you pass --login. If you source .profile, then bash will not source .bashrc.

I'd try to put:

[ -f $HOME/.bashrc ] && source $HOME/.bashrc

in your .profile, see if that helps. Your .bashrc should still be sourced, as normal, in other non-login shell invocations.

And don't pass -i to bash; you don't need it. Bash kind of hacks that in, pretends you gave -i on the command line.

Solution 3:

I experienced the same behavior - probably after having git (Windows) installed.

In the system setting I've found the user variable HOME=C:\Users\me and reset this to HOME=D:\home