1

When running cygwin, the shortcut I have uses the following:

C:\cygwin\bin\bash.exe --login -i

What is unclear to me is why bash.exe executes twice. I see two processes running in task manager. If I put an echo statement echo .Bashrc file has been processed at the bottom of my .bashrc file I will see that echo output appear twice in the console window.

enter image description here

What is it about bash.exe --login that causes the process to run twice? (is this expected behavior?)

  • You may want to include the contents of your .bash_profile (or .profile) file as well as .bashrc in the question. I'm guessing that one of them is starting a new bash process for some reason. – Kusalananda Sep 14 '17 at 22:45
  • oh cripes.. good call. I didn't look in the .bash_profile. smacks forhead Sure enough I have this : [[ -z $SSH_AGENT_PID && -z $DISPLAY ]] && exec -l ssh-agent $SHELL -c "bash --login". .bash_profile must only get called when --login is used? – Marcel Wilson Sep 14 '17 at 22:50
  • That's a correct understanding. – Kusalananda Sep 14 '17 at 22:56
3

A bash shell that is a login shell will source ~/.bash_profile.

The ~/.bash_profile file on Marcel's Cygwin system contained the line

[[ -z $SSH_AGENT_PID && -z $DISPLAY ]] && exec -l ssh-agent $SHELL -c "bash --login"

This will start ssh-agent if no such agent is running in the current shell environment and if there's no X11 display available. The SSH agent will start two new bash shells, a non-interactive shell and a login shell (the login shell will skip over the above line since it will have $SSH_AGENT_PID set). The SSH agent replaces the shell it's starting from by virtue of being started with exec.

The two bash processes that may be seen are

  1. The one started by ssh-agent (this is $SHELL -c), and
  2. The one started by the $SHELL -c, invoking bash --login.

To me, it looks like the line could be simplified down into

[[ -z $SSH_AGENT_PID && -z $DISPLAY ]] && exec -l ssh-agent bash --login

That would get rid of the extra shell that ssh-agent uses just for running bash --login.

-1

@Kusalananda pointed out that --login triggers .bash_profile to be called. My .bash_profile had the following line

[[ -z $SSH_AGENT_PID && -z $DISPLAY ]] && exec -l ssh-agent $SHELL -c "bash --login"

thus a second bash was being opened everytime.

  • 1
    It's slightly more complicated than that :-) The shell you start stops existing at the exec and is replaced by ssh-agent. The agent starts $SHELL (bash) and then that bash issues the bash --login command. – Kusalananda Sep 14 '17 at 23:11
  • Right, but that doesn't happen if I don't use --login, thus --login is the triggering effect as to why I would see two bash processes, no? – Marcel Wilson Sep 15 '17 at 0:01
  • Correct again. Unless ~/.bash_profile is sourced by ~/.bashrc, it won't be read by a non-login bash. – Kusalananda Sep 15 '17 at 6:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.