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

9

3

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

gsingh2011

Posted 2012-02-29T07:02:06.757

Reputation: 863

When your bash shell starts (and puts you in your Windows home directory), what does echo $HOME print? – Keith Thompson – 2012-03-01T00:44:35.517

It echoes my Windows home, not my Cygwin home. – gsingh2011 – 2012-03-01T00:52:09.783

But using Cygwin syntax, right? (/cygdrive/c/Users/foo rather than C:\Users\foo) – Keith Thompson – 2012-03-01T00:55:09.457

Yes, using Cygwin syntax. – gsingh2011 – 2012-03-01T00:56:25.793

What happens when you type HOME=/home/yourname bash -l at the terminal prompt? If I'm right, this should give you properly working shell. Note that this is a diagnostic, or at best a workaround, not a solution; we still need to figure out why your $HOME is incorrect in the first place. The root problem is the incorrect setting of $HOME; bash is behaving correctly. – Keith Thompson – 2012-03-01T00:57:45.787

Yes, that command works, thanks. How can we go about debugging what changed the $HOME variable? – gsingh2011 – 2012-03-01T01:01:24.673

See my answer.. – Keith Thompson – 2012-03-01T01:05:52.347

Answers

9

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)?

Keith Thompson

Posted 2012-02-29T07:02:06.757

Reputation: 4 645

I've added the output to my question so it's easier to read. It looks like it's set correctly there. However, I opened up my Windows Environment variables to see if there was a %HOME% variable and there was and it was set to %USERPATH%. I removed it, but it didn't solve my issue. Maybe it needs a restart? – gsingh2011 – 2012-03-01T01:14:28.023

1Did you remove it from the System control panel, or from a Windows command shell using set HOME=? Using the System control panel is more likely to work, but a restart might be necessary even then. – Keith Thompson – 2012-03-01T01:19:45.320

I removed it by right clicking on Computer in the start menu, clicking on properties, clicking on advanced system settings, then environment variables, and then removing HOME. A restart was necessary. Now Cygwin loads the right home directory. Thanks for the help. – gsingh2011 – 2012-03-01T01:31:10.453

1

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.

Rich Homolka

Posted 2012-02-29T07:02:06.757

Reputation: 27 121

My .bash_profile and .profile have similar lines of code. See my updated question. – gsingh2011 – 2012-03-01T00:14:47.410

Excuse me, @Rich Homolka, but I would like to know why do you say that -i is not necessary. Thanks. – Sopalajo de Arrierez – 2014-03-04T14:41:46.583

@SopalajodeArrierez if you have an interactive (not a script, but active at a terminal) shell, bash pretends you gave it -i on the command line. Try opening a bash shell window, then echo $-, you should see i (among possibly other flags) – Rich Homolka – 2014-03-04T15:07:23.707

1

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

user377223

Posted 2012-02-29T07:02:06.757

Reputation: 11