Home > Blockchain >  Why does canceling a Windows batch script with Ctrl C when prompted by "set /p" give incon
Why does canceling a Windows batch script with Ctrl C when prompted by "set /p" give incon

Time:12-21

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:

  1. ^C appears, followed by Terminate batch job (Y/N)?
  2. ^C appears, followed by The syntax of the command is incorrect. Then the script terminates, without echoing bye.
  3. ^C appears and the script continues, echoing bye.

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: enter image description here

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 enter image description here

  • Related