Is it safe to write batch files with Unix line endings?

I think I ran across a bug in a batch file because it was written with unix line endings. Is this a known problem with cmd.exe running batch files in windows?


Solution 1:

This really isn't a "bug"... as it is by-design. Windows new-lines are defined as "\r\n" ... or a "Carriage Return" and "New Line" combination... whereas *nix flavors prefer omitting the carriage return. You should always use "\r\n" in anything in Windows where possible. Anything else may be interpreted incorrectly... and cause a great deal of unexpected results.

Solution 2:

It seems that batch labels are skipped when LF (Unix line-ending) is used in a .bat file.

Solution 3:

For batch files there is no big difference between unix line endings and windows line endings.

Currently, there is only a glitch known for the behavior of GOTO and CALL, while searching for a label. The label parser fails to find a label, if the label is on a 512 byte boundary relative to the current file position. The difference is, because the carriage return is used for an internal line reset. See Call and goto may fail when the batch file has Unix line endings

Despite the label scanner, it's unexpected to find more problems, because the batch parser removes all carriage returns directly after the percent expansion phase.

Sample to show the problems:

@echo off

goto :main

:func
echo ************************************************
echo ************************************************
echo ************************************************
echo ************************************************
echo ************************************************
echo ***********************************************
echo ***********************************************
echo ***********************************************
echo ************************************************
echo never go back to :main
echo This is the end of :func
exit /b

:main
:main
echo This is main
goto :func
exit /b

The output is

This is the end of :func

Solution 4:

Kinda, but...

You will have to be a paranoid and

  • always duplicate your label lines!
  • never use colons outside of label definitions!

Eg.:

@echo off
goto main <- No use of colon
...

:main
:main <- Repeat label
echo At least one of the above labels are discoverable

REM main part done <- No use of colons in comments

How it works

When using Unix line endings, the label parser will skip over some labels because of an off-by-one error. This is due to the parser's use of 512-byte sized chunks and the assumption that line endings is denoted by two characters, \r\n, rather than one, \n. When a label is erroneously skipped over, the next erroneous skip can only occur at an offset of 512 bytes. If you duplicate a label on the next line, the duplicated label will be within the 512 limit, and can act as a fallback.

Furthermore, as demonstrated by @jeb, the parser also misinterprets the end of each 512-byte size chunk as a new line (as well as somehow ignoring white space characters between a colon and the next text on these pseudo lines). A comment such as :: main section can trigger the parser into somehow reading the text : main as the label :main.

In summary, not only can the parser skip labels, it can also misinterpret comments and other pieces of text as labels.

Why not just stick with dos line endings?

It's not always safe to assume that your batch script will retain its line endings, especially when using Git or sharing content over GitHub. It's also convenient for cross platform projects not to care about line endings and assume Unix line endings as a common denominator.

Solution 5:

The answer is: you may get "lucky" and it will work with LF, but don't count on it. We had same problem as the original requestor. Our process would end up with bat files that were only LF, and (sorry could not find a pattern) sometimes a label would be 'not found', though clearly it was there. Would have to convert to CR-LF, or make random changes until it worked!