Robert Mark Bram Robert Mark Bram - 3 months ago 18
Bash Question

Exit from cmd script that calls bash

I have the following cmd script which calls a cygwin bash script:

C:\cygwin\bin\bash -l /D/Temp/testScript/
echo back in cmd.

The bash script is simple:

echo Hello World!

The calling part works nicely - the bash shell logs in, echos as expected, reads as expected and control passes back to the cmd as expected.

But the cmd will not exit. This is fine if I run it from a command window, but I will be calling this by double clicking on the cmd file or launching it from RUN etc.

Output I see:

D:\Temp\testScript>C:\cygwin\bin\bash -l /D/Temp/testScript/
Hello World!
D:\Temp\testScript>echo back in cmd.
back in cmd.

How do I get the cmd to exit?

Found the problem - too many bashes

I think I found the issue - probably something in my .bashrc and other files I load during cygwin login. If I change the main cmd line to remove the login flag), it works as expected - everything closes.

C:\cygwin\bin\bash /D/Temp/testScript/

But then I put the flag back:

C:\cygwin\bin\bash -l /D/Temp/testScript/

and run again. I see the output
Hello World!
which shows me that control is with bash, and I check Task Manager. Four instances of bash.exe are created. Then I press ENTER and see the output
back in cmd.
showing me that control is back with cmd. Now Task Manager shows me that three bash.exe instances remain.

So, something in my login scripts are creating extra bash shells. So it's not you, it's me.


Avoid creating persistent subshells in login scripts by using cygstart instead of cmd.

Previously I wrote that the problem was too many bashes. My .bash_profile was doing something to create bash sub-shells. This meant that when my cmd created a bash (via login) and the first bash exited, the sub-shells didn't exit.

I found that part of my login scripts involved launching some Autohotkey scripts like this:

cmd /c "$thePath" &

The fix was so easy... just use cygstart:

cygstart "$thePath"