This is the script foo.cmd :
@echo off
echo hi
set /p foobar="???"
echo bye
When prompted by set /p
, I press Ctrl C
to cancel the script. At this point, one of several possible things happens, seemingly chosen at random:
^C
appears, followed byTerminate batch job (Y/N)?
^C
appears, followed byThe syntax of the command is incorrect.
Then the script terminates, without echoingbye
.^C
appears and the script continues, echoingbye
.
I have also seen cases like 1 or 2 but where the ^C
appears after the "Terminate" prompt or syntax error message.
Actual output of three consecutive attempts:
C:\Users\Me\Documents>foo.cmd
hi
???^CThe syntax of the command is incorrect.
C:\Users\Me\Documents>foo.cmd
hi
???^Cbye
C:\Users\Me\Documents>foo.cmd
hi
???^CTerminate batch job (Y/N)? y
C:\Users\Me\Documents>
I found one related question from 7 years ago, but the answers didn't include any explanation of why this happens:
As an attempt to prove that CTRL C
is killing the internal processes in your example, then the main process, we can create CTRL C
in the script itself (All credit goes to user