curiosity about echo command in prompt

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
carlos
Expert
Posts: 501
Joined: 20 Aug 2010 13:57
Location: Chile
Contact:

curiosity about echo command in prompt

#1 Post by carlos » 14 Feb 2014 05:26

Hello.
I'm curious about why happens this in Windows 8 in cmd in interactive mode:

this script:

Code: Select all

@echo off
echo h


after the h there are a 0d 0a character, it prints:

Code: Select all

C:\dev>test.cmd
h

C:\dev>


but adding other 0d 0a character:

Code: Select all

@echo off
echo h




it prints:

Code: Select all

C:\dev>test.cmd
h
C:\dev>

Endoro
Posts: 244
Joined: 27 Mar 2013 01:29
Location: Bozen

Re: curiosity about echo command in prompt

#2 Post by Endoro » 14 Feb 2014 06:02

to avoid this put EOF at the end of file:

Code: Select all

C:\TEST>xxd -g1 test.cmd
0000000: 40 45 43 48 4f 20 4f 46 46 0d 0a 45 43 48 4f 20  @ECHO OFF..ECHO
0000010: 68 0d 0a 1a                                      h...

C:\TEST>test
h
C:\TEST>

dbenham
Expert
Posts: 2397
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: curiosity about echo command in prompt

#3 Post by dbenham » 14 Feb 2014 08:36

It has nothing to do with ECHO. The behavior is strictly controlled by how the batch script ends. This is even true for a batch script without any commands :!:

There are two possible outcomes when batch processing terminates:

outcome 1 - There is an extra blank line above the prompt that appears after the batch output.

outcome 2 - The prompt appears directly below the batch output without an extra blank line


Here are some scenarios that yield outcome 1:

1) an empty script

2) The script contains a single line without linefeed (0A). The line consists solely of token delimiters and/or carriage returns (0D).

3) The last command is not terminated by 0A or 1A.

3) The last command is terminated by a single 0A or 1A. There may be a subsequent line containing token delimiters and/or 0D, but there are no additional 0A or 1A.


Here are some scenarios that yield outcome 2:

1) The file consists of a single line consisting of 0 or more whitespace characters and/or token delimiters, terminated by 0A or 1A

2) The last line is a label, or a label hack comment

3) The last command is terminated by 0A or 1A, and at least one additional 0A or 1A follows. There may or may not be token delimiters or 0D on the subsequent lines.


Note that the batch parser largely treats 1A the same as 0A


Dave Benham

Post Reply