Cygwin64: Confused about when /etc/bash.bashrc is run?

1

I have Windows 7 64-bit PC. I have installed Cygwin version 2.6.0 on it in c:\cygwin64 directory.

I am confused that when I open the Cygwin64 Terminal then does configuration file /etc/bash.bashrc run?

I think this is an interactive login shell, so as per my understanding /etc/bash.bashrc is never run. Here is the link of my understanding: https://shreevatsa.wordpress.com/2008/03/30/zshbash-startup-files-loading-order-bashrc-zshrc-etc/

But in the /etc/bash.bashrc file I can see environment variable PS1 is set. And it is the same value I get when I use echo $PS1 command. So does that mean /etc/bash.bashrc did run?

I may not be understanding correctly "interactive login" and "interactive non-login". Any help is greatly appreciated.

Thanks

ChumboChappati

Posted 2017-01-21T23:49:38.960

Reputation: 167

If you use bash from script when it called as #!/bin/bash it is non interactive and bash won't pick up .bashrc, but if you run bash as seating in console then it is interactive and .bashrc loaded in this case – Alex – 2017-01-22T00:09:11.097

@Alex So whether its interactive login or interactive non-login, the .bashrc is read? – ChumboChappati – 2017-01-22T00:24:04.533

I will answer in "answer" to avoid restriction on maximum characters per comment – Alex – 2017-01-22T08:09:25.663

Answers

1

When I open the Cygwin64 Terminal then does /etc/bash.bashrc run?

Short Answer:

Yes. I added some echo statements to the bash startup files and opened a Cywin64 Terminal. Here is the output:

/etc/bash.bashrc
/home/DavidPostill/.bash_profile
.profile
/home/DavidPostill/.bashrc
/home/DavidPostill/.bashrc
$

For some reason ~/.bashrc is called twice, at the moment I'm not sure why.


Long Answer:

The shortcut for the Cygwin64 Terminal runs the following command:

C:\cygwin\bin\mintty.exe -i /Cygwin-Terminal.ico -

The help for mintty states:

$ mintty --help
Usage: mintty [OPTION]... [ PROGRAM [ARG]... | - ]

Start a new terminal session running the specified program or the user's shell.
If a dash is given instead of a program, invoke the shell as a login shell.

So we can see the the trailing - on the shortcut command causes a login shell to be run.

What happens next depends on what your login shell is set to.

Assuming it set to bash then:

  • If it is a login shell Cygwin will execute ~/.bash_profile if it exists or otherwise ~/.profile.

    Note that .bash_profile will execute ~/.profile

  • If it is an interactive shell Cygwin will execute ~/.bashrc

.profile (other names are also valid, see the bash man page) contains bash commands. It is executed when bash is started as login shell, e.g. from the command bash --login. This is a useful place to define and export environment variables and bash functions that will be used by bash and the programs invoked by bash. It is a good place to redefine PATH if needed. We recommend adding a ":." to the end of PATH to also search the current working directory (contrary to DOS, the local directory is not searched by default). Also to avoid delays you should either unset MAILCHECK or define MAILPATH to point to your existing mail inbox.

.bashrc is similar to .profile but is executed each time an interactive bash shell is launched. It serves to define elements that are not inherited through the environment, such as aliases. If you do not use login shells, you may want to put the contents of .profile as discussed above in this file instead.

Source Customizing bash

DavidPostill

Posted 2017-01-21T23:49:38.960

Reputation: 118 938

0

This answer explains the difference between login and non-login quite nicely. To summarize, under Cygwin, you are already logged in and just opening a new terminal window. It is the same as if you opened a terminal emulator on a linux desktop or were using screen.

CombatBotanist

Posted 2017-01-21T23:49:38.960

Reputation: 1

So for interactive non-login .bashrc is read, but for interactive login .bashrc is not read ? – ChumboChappati – 2017-01-22T00:28:35.620

@ChumboChappati No, in both cases .bashrc will be read because it is interactive session. The only case when .bashrc won't be read - it is non interactive invocation – Alex – 2017-01-22T09:07:33.933

0

When you opening cygwin's terminal, it is always " interactive login". See c:\cygwin\cygwin.bat that contain bash --login -i.

According to bash's manual:

"If the -i option is present, the shell is interactive."

and "--login" is

"Make bash act as if it had been invoked as a login shell".

What you asking about /etc/bash.bashrc - it is system wide initialization file bashrc that can be overridden by local ~/.bashrc or applied in case users directory doesn't have ~/.bashrc. When you already in cygwin's terminal and will try to run bash - it is non-login since you already logged in, but it is interactive session because you called bash not from script. If you will call bash from script as

#!/bin/bash

echo "Hello SuperUser"

then it is non interactive and non login invocation. This way bash won't read any bashrc at all.

Read section "invocation" in official bash manual to better understand how bash interpret ~/.bash_profile, ~/.bash_login, ~/.profile, /etc/bash.bashrc, ~/.bashrc and order it used.

Alex

Posted 2017-01-21T23:49:38.960

Reputation: 5 606