Windows CRLF to Unix LF Issues in Vagrant

You're right, the documentation about binary was misleading. I've proposed a pull-request and this has been corrected on the documentation page.

So now it states:

binary (boolean) - Vagrant automatically replaces Windows line endings with Unix line endings. If this is false, then Vagrant will not do this. By default this is false. If the shell provisioner is communicating over WinRM, this defaults to true.

So to replace Windows line endings (CRLF) with Unix line endings (LF) you need to set:

s.binary = true

Alternative solutions includes:

  • Changing line endings manually by:

    • using dos2unix command,
    • using ex command, e.g.

      ex +'bufdo! %! tr -d \\r' -scxa *.sh
      
  • Add the following lines into your Bashrc file (e.g. ~/.bashrc)gist:

    export SHELLOPTS
    set -o igncr
    

If you're using Git for versioning your code, you should:

  • Configure Git on OS X to properly handle line endings by setting core.autocrlf option to input or false.

    If you've installed Git On Windows, the most common mistake is to select Checkout Windows-style option during installation, so you should re-install it and choose either: Checkout as-is and commit Unix-style line endings (core.autocrlf is set to input) or Checkout as-is, commit as-is (core.autocrlf is set to false).

  • Consider creating git normalization file in your repository (.gitattributes) which ensures no CRLF line-endings are in place, neither on checkout nor on checkin, for example:

    *.sh     text eol=lf
    

    So people editing your provisioning script, they won't break the line endings.

  • Read also: Dealing with line endings at GitHub Help.

  • Related: '\r': command not found - .bashrc / .bash_profile.

As was specified above in my update, changing s.binary = true fixed the issue. However, I think the wording in the documentation should be re-addressed. The documentation states that "If this [the flag] is true, then Vagrant will not do this [change CRLF to LF]." As I understand then, Vagrant will not change CRLFs to LFs if this flag is set. However, CRLFs are changed to LFs if this is set to true.