15

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?

2
  • 1
    I have never had a problem, and I always use unix line endings. But that doesn't mean that there isn't some problem I have just been lucky enough to miss. – Zoredache Sep 18 '12 at 19:19
  • 2
    Which bug did you run into? Commands failing to start? Or just that it looked as if there are not line endings when you open the file with notepad? – Hennes Sep 18 '12 at 19:21
12

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.

4
  • I came across an issue where call :label didn't jump to a label, but the same call later on the file did jump to the label. The file in question was using \n line endings. I switched to \r\n line endings for all batch files in my project and the problem was gone. I didn't re-test with \n endings to confirm it wasn't just a one-time fluke (the scripts takes nearly an hour to run). – Samuel Jun 19 '15 at 22:28
  • 2
    Notepad can't handle Unix line endings, but WordPad can, PowerShell can, many other things can as well. – Michael Hampton Aug 8 '16 at 21:54
  • Cam you provide some more examples when .bat files do not work correctly with nix line endings? – Michal Kordas Apr 29 '19 at 18:19
  • @MichalKordas Without going through and testing every version of every Microsoft OS and *nix distribution, that's sort of a tall order to give. This really isn't the place to go into a detailed discussion on the subject. Best practices are such that Windows uses \r\n, *nix uses \n, and sometimes mac uses just a \r. Since Only Windows uses batch files, it's best to assume you should use \r\n. – TheCompWiz May 2 '19 at 15:40
4

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

4

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.

2
1

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!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.