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?

  • 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.

| improve this answer | |
  • 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.

| improve this answer | |
3

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

goto, call or even the creation of linefeed variables works with both styles.

And as the batch parser removes the carriage returns directly after the percent expansion phase, they will never play a big role.

| improve this answer | |

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.