How to keep restarting Windows Command Prompt
Cygwin bash is often preferred over the Windows command line shell, so we use it to set up our environment before the Windows shell came along. However, terminating the execution of the current process in that spawned shell with Ctrl-C kills the user back to the bash shell.
My attempt at 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-Cs are not caught. Is there a way to resume the cmd process?
source to share
Subsequent Ctrl-Cs are not caught because your script terminates due to reaching the end.
Most likely cmd will return an error when you ctrl-c, in which case you could do
until cmd /k; do true; done
Otherwise, create a loop script until ctrl-c is pressed:
trap restart=1 SIGINT
echo -e " === Starting windows cmd prompt\n"
restart=1
while (( restart )); do restart=0; cmd /k; done
source to share
To complement the helpful "Unhappy Variable" answer with an explanation of why opening cmd.exe
in a custom console window is the best approach :
-
While you can try to work around the issue Ctrl-C, as shown in what the other guy's answer shows , the behavior is not quite the same as in a regular
cmd.exe
console window: since a new instancecmd.exe
is created every time you click Ctrl-C, the previous state is lost. -
Cygwin terminal (console window) uses UTF-8 character encoding, not the standard console window encoding which is based on the (old) OEM code page, and the two are incompatible.
-
Curiously, staying in the Cygwin terminal causes
cmd.exe
all interactively presented commands to echo (as you get into a batch file without@echo off
).
To automatically close the Cygwin window when the console window starts cmd.exe
, use
exec cygstart cmd
.
source to share