Cygwin doesn't find /.bashrc

12,628

Solution 1

I had the same problem on my Cygwin installation.

CYGWIN_NT-6.1 2.5.2(0.297/5/3) x86_64 Cygwin

I created a ~/.bashrc file in my (cygwin) home directory, but the aliases I set up did not work.

The problem was that I did not have a ~/.bash_profile file in my home directory. It must contain the following line to recognize the desired .bashrc

. ~/.bashrc

I created it by appending the needed line

echo ". ~/.bashrc" >> ~/.bash_profile

See StackOverflow > Cygwin shell doesn't execute .bashrc

Solution 2

Just to clarify: this is has nothing to do with Cygwin. It is normal Bash behavior.

~/.profile: The login script filename originally used by /bin/sh.

~/.bash_profile: The personal initialization file, executed for login shells

~/.bashrc: The individual per-interactive-shell startup file.

Each new Cygwin window (each invocation of Cygwin.bat) opens a login shell, because there is no init process and you are already logged in as Windows user. In Cygwin.bat Bash is invoked so: bash --login -i.

Because of --login the ~/.bash_profile is executed. More specifically, Bash reads /etc/profile and then ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, and executes the first one that is readable.

Because of -i the ~/.bashrc should be executed too. But it isn't because Bash only executes .bashrc for a shell that's both interactive and non-login.

Note that bash --login -i --rcfile=C:/Cygin/home/%USERNAME%/.bashrc won't work either, because --rcfile is silently ignored in case of login shells.

Therefore it is good practice to hook $HOME/.bashrc in case of a true login shell (textual login shell). This is the reason why many .bash_profile files contain the following lines:

if [ -f "${HOME}/.bashrc" ]; then
    source "${HOME}/.bashrc"
fi

Textual login shells appear when you ssh/telnet to a host, boot Linux into text mode or... run Cygwin.bat.

Having come this far I'd like to add a subtle point. Many Linux desktops read ~/.profile (not ~/.bash_profile or ~/.bash_login) automatically by the Display-Manager during the start-up process desktop session. Therefore hook ~/.bashrc in ~/.profile too:

if [ -n "$BASH_VERSION" ]; then
    if [ -f "$HOME/.bashrc" ]; then
        . "$HOME/.bashrc"
    fi
fi

Because Bash executes ~/.profile in absence of ~/.bash_profile you may get away with only ~/.profile.

Now you can do all customization in ~/.bashrc.

Share:
12,628
tonni
Author by

tonni

Updated on June 04, 2022

Comments

  • tonni
    tonni almost 2 years

    I install Cygwin on Windows 7 64bit, and my location on /.bashrc location is C:/cygwin64/home/admin/bashrc, but I can't see it from Cygwin, it's says:

    bash: /.bashrc: No such file or directory

    What I try is to navigate to that folder with the command:

    cd /cygdrive/c/cygwin64/home/admin/
    

    and then use:

    /.bashrc
    

    but it says:

    No such file or directory

    What to do to see that file?

  • Ezry
    Ezry over 3 years
    Note: if your .bash_profile ends in a comment line instead of a newline, the appended line may end up as a comment.