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?

+3


source to share


3 answers


Do I need to be in the same window? If not, I'm much luckier with

cygstart cmd

      



cmd

starts in its own window; and only exit closes this window

+2


source


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

      

+1


source


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 instance cmd.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

.

0


source







All Articles