18

When I enter brew doctor (or any brew command) in my terminal, I get this as a response:

-bash: /usr/local/bin/brew: /bin/sh^M: bad interpreter: No such file or directory

I have seen the ^M response before and I think it has to do with dos line ending files. Is there a way to fix this?

mklement0
  • 312,089
  • 56
  • 508
  • 622
  • 2
    This happened to me, and I figured out why. I had copied my .gitconfig over from my windows machine, and didn't change the core.autocrlf to input before installing brew. D'oh! I think Javier Roca's solution is the nicest. – Kevin Pauli Mar 27 '15 at 02:11
  • 1
    Possible duplicate of [./configure : /bin/sh^M : bad interpreter](http://stackoverflow.com/questions/2920416/configure-bin-shm-bad-interpreter) – mklement0 Dec 12 '16 at 14:54
  • Possible duplicate of [Bash script: bad interpreter](https://stackoverflow.com/q/2841593/608639) – jww Oct 19 '18 at 03:27

2 Answers2

37

This worked for me:

  1. Open file /usr/local/bin/brew with vi (vi /usr/local/bin/brew)
  2. While on vi issue this commad (via esc or :) :set fileformat=unix
  3. Close file on vi via :wq!

The brew command should be OK now.

Regards.

that other guy
  • 109,738
  • 11
  • 156
  • 185
Javier Roca
  • 371
  • 3
  • 2
35

I don't know how carriage returns ended up in your brew file, but you can remove them using dos2unix or by piping it through tr -d '\r'.

Example:

tr -d '\r'  < /usr/local/bin/brew   > myfixedbrew

Once verified, you can use

mv myfixedbrew /usr/local/bin/brew && chmod a+x /usr/local/bin/brew 

to replace the old one.

that other guy
  • 109,738
  • 11
  • 156
  • 185
  • How would I use tr -d '\r'? Should I cd to /usr/local/bin then use it on the brew file? –  Apr 16 '14 at 01:47
  • 1
    @mah wouldn't I need to use brew to install dos2unix? –  Apr 16 '14 at 01:50
  • Hrm. Sorry, I didn't realize dos2unix wasn't already on OS X. – mah Apr 16 '14 at 01:52
  • Ok @thatotherguy I used that command and now I am coming up with a new error: "sudo: brew: command not found". I am thinking about removing all brew then installing it again but I get this:"bash: /usr/local/bin/brew: Permission denied" when I try to delete it using: "sudo rm -rf /usr/local/Cellar /usr/local/.git && brew cleanup" –  Apr 16 '14 at 01:54
  • @TheWaller I neglected to mention that you'd have to add back execute permissions on the file – that other guy Apr 16 '14 at 01:56
  • Thank you, finally I could install brew – Carlos Goce Sep 27 '14 at 15:47
  • this happened to me too all i did is run ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" from terminal as instructed – Alex Borsody Mar 29 '15 at 00:35
  • That fixes it. It seems the Ruby install script causes this issue on Mac OS X (just tried on Yosemite) – Jorge Orpinel Pérez Jun 12 '15 at 20:11
  • awesome that fixed it. – Damodar Bashyal Sep 05 '15 at 07:20
  • Bless on you, you helped me out. That was such a strange an uncomfortable issue for me – Bandydan Oct 15 '15 at 11:59
  • After how many decades of carriage return & line feed debates, why do we still have interpreters that even care about the difference!? – Bron Davies Mar 02 '16 at 17:53