Question

Cygwin's bash is often preferable to Windows' cmd command shell, so we use it to set up our environments before spawning a Windows shell. However, halting execution of a running process in this spawned shell with Ctrl-C kills boots the user back to the bash shell.

My attempted workaround:

source setupEnvironment.sh

restartCommand() {
  # Reset trap
  trap restartCommand SIGINT
  echo -e " === Restarting windows cmd prompt\n"
  cmd /k 
}

trap restartCommand SIGINT
echo -e " === Starting windows cmd prompt\n"
cmd /k

This approach only restarts cmd once. Subsequent Ctrl-C's are not caught. Is there a way to keep restarting the cmd process?

Était-ce utile?

La solution

Does it have to be in the same window? If not, I have had much better luck with

cygstart cmd

cmd starts in its own window; and only exit closes that window

Autres conseils

Subsequent Ctrl-Cs aren't caught because your script exits due to reaching the end.

Chances are cmd will return error when you ctrl-c, in which case you could do

until cmd /k; do true; done

Otherwise, make your script loop, until ctrl-c isn't pressed:

trap restart=1 SIGINT
echo -e " === Starting windows cmd prompt\n"
restart=1
while (( restart )); do restart=0; cmd /k; done

To complement Miserable Variable's helpful answer with an explanation of why opening cmd.exe in its own console window is the best approach:

  • While you can try to work around the Ctrl-C issue as demonstrated in that other guy's answer, the behavior is not quite the same as in a regular cmd.exe console window: since a new instance of cmd.exe is created every time Ctrl-C is pressed, previous state is lost.

  • The Cygwin terminal (console window) uses the UTF-8 character encoding rather than the regular console window's encoding that is based on the (legacy) OEM code page, and the two are incompatible.

  • Curiously, staying in the Cygwin terminal causes cmd.exe to echo all interactively submitted commands (as you'd get in a batch file without @echo off).

To automatically close the Cygwin window when launching the cmd.exe console window, use
exec cygstart cmd.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top