5

I've been playing around with writing batch files on my computer for some time, and I am confused about why PAUSE doesn't work like I thought(maybe I'm just doing it wrong idk, just really confused).

Basically I have:

@echo off
echo Hello!
pause
echo Hi again!
pause
echo HEY HEY!!
pause

and when it runs I get:

Hello!
Press any key to continue...

I then press just one key and this occurs:

Hi again!
Press any key to continue...
HEY HEY!!
Press any key to continue...

I don't understand why it skips the second PAUSE...I've noticed that if I was to do:

@echo off
echo Hello!
pause
echo Hi again!
pause
pause
echo HEY HEY!!
pause

then it will pause on "Hi again!" opposed to executing it without pause(however it will print "Press any key to continue..." two times consecutively)

I'm just really lost and can not proceed with my life until I know why it does this o:

4
  • It works as expected for me (in Windows 7), you sure you don't have a keyboard problem? If you wrap the echo argument in quotes does it work differently? (ie: echo "Hi there!", echo "Hi Again", etc.) Oct 31, 2014 at 16:57
  • Let me try that now...and I have no keyboard problem, it will only skip that pause, and none other Oct 31, 2014 at 17:09
  • 2
    You know, I feel really stupid, any key I hit other then the down arrow makes it work fine.....jeez I can't believe I've spent 2 hours trying to figure this out for nothing xD Oct 31, 2014 at 17:11
  • 3
    Yup, hitting any arrow key does the same thing for me that you saw as well. Good catch. Get in the habit of using Enter. ;) Oct 31, 2014 at 17:15

2 Answers 2

1

Probable, simple explanation:

If you press a "special" key¹, then the actual characters sent into the "stdin" file of the software² running in the selected window will be, not just one character, but several.

The ACTUAL characters sent depend on the software setup and possibly "terminal emualation"³ - for starters

¹) e.g. f1, ..., f12, cursor movement and probably some more similar (home, end, ...?)
²) i.e. cmd.exe or anything similar
³) e.g. ancient ansi.sys having been loaded or not


Proof, creating, compiling a simple c program (Bash, gcc, Linux):

$ cat showhex.c 
#include <stdio.h>

int main(void) {

  int c=0;
  char hex[]="0123456789abcdef";
  while ( ! feof(stdin) ) {
    c=fgetc(stdin);
    fputc(hex[ (c >> 4) & 0x0f], stdout);
    fputc(hex[  c       & 0x0f], stdout);
    fputc(' ', stdout);

  }
}

$ gcc -o showhex showhex.c 

$ chmod 755 ./showhex

$ ls -hl showhex
-rwxr-xr-x 1 hannu hannu 8,7K dec  5 23:01 showhex

$ ./showhex
^[[A
1b 5b 41 0a ^[[C
1b 5b 43 0a ^[[B
1b 5b 42 0a ^[[D
1b 5b 44 0a ff 
$ 

Pressing a key shows the actual characters first, then as you press ENTER, the corresponding hex codes get printed.

Hold CTRL and hit D (CTRL+D, bash) or CTRL+Z and then ENTER (cmd.exe) to stop

The above example is from pressing
Cursor up, Enter
Cursor right, Enter
Cursor down, Enter
Cursor left, Enter
CTRL+D

1
  • Some comments elsewhere indicate the problem happened when using an arrow key. Arrow keys do generate multiple characters with at least some terminals, so this answer specifies the actual cause.
    – TOOGAM
    Dec 5, 2019 at 21:44
0

It's working as expected. The pause statement just waits for input. The only switch is to hide the fact it's waiting for a keypress.

When I run the last batch file you showed, I see this:

C:\Users\asdf\Desktop>more test.bat
@echo off
echo Hello!
pause
echo Hi again!
pause
pause
echo HEY HEY!!
pause

C:\Users\asdf\Desktop>test
Hello!
Press any key to continue . . .
Hi again!
Press any key to continue . . .
Press any key to continue . . .
HEY HEY!!
Press any key to continue . . .

So I'm finding it difficult to understand what you're asking for...

4
  • That doesn't explain why the second statement is being skipped.
    – Jim G.
    Oct 31, 2014 at 16:55
  • 1
    I shouldn't need 2 consecutive PAUSE statements after "Hi again!" I'm basically asking why 1 PAUSE isn't working. Oct 31, 2014 at 17:03
  • That's how it runs on Windows 7... Oct 31, 2014 at 17:08
  • @JimG. As you can see from the OP's comments above, it is the expected behaviour Oct 31, 2014 at 17:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .