Weird $PATH on macos results in brew failure

Recently, when I attempted to run brew update on my MBP running Mojave 10.14.6, I got the following error:

$  brew update
/usr/local/bin/brew: line 16: /sw/sbin/readlink: Bad CPU type in executable
/usr/local/bin/brew: line 16: /sw/sbin/readlink: Bad CPU type in executable
/bin/bash: /usr/local/Library/Homebrew/brew.sh: No such file or directory

Line 16 of the /usr/local/bin/brew script is:

  target="$(readlink "$1")"

Without my knowledge, something had changed my $PATH to:

/opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/sw/bin:/sw/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin

Sleuthing followed:

$ which readlink
/sw/sbin/readlink
$ file /sw/sbin/readlink
/sw/sbin/readlink: Mach-O executable ppc
$ ls /usr/bin/readlink
/usr/bin/readlink
$ file /usr/bin/readlink
/usr/bin/readlink: Mach-O 64-bit executable x86_64
$ echo $PATH
/opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/sw/bin:/sw/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
$ grep sw .bashrc
$ grep sw .profile
test -r /sw/bin/init.sh && . /sw/bin/init.sh

I edited /usr/local/bin/brew line 16 to:

  target="$(/usr/bin/readlink "$1")"

…and problem solved. I have no idea how /sw/bin and /sw/sbin got prepended to /usr/local/bin on my $PATH, let alone why Mojave still includes PPC-compiled binaries, but, well, there it is.

I don’t normally post to forums, but searching on the error got me zero, so I thought I’d leave this here in case anyone else hits the same freak corner case.

1 Like

Did you install Fink at some point? It’s a different package manager which installs to /sw; another is MacPorts which installs to /opt.

1 Like