MacOS – Executables in /bin and /usr/bin are not found in path

bashmacosterminal

Somehow I can’t execute files in /bin or /usr/bin without providing the full path.

This isn’t happening when running from Terminal, but, for example, iTerm can’t run bash (only /bin/bash), OnyX can't run sw_vers.

.profile: export PATH=/opt/local/bin:/opt/local/sbin:/bin:/usr/bin:$PATH 
.profile: export PATH=$PATH 
.bash_history: export PATH="$PATH:"'/Users/gilstrauss/Applications/CrossOver.app/Contents/SharedSuppor‌​t/CrossOver/bin' 
.bash_history: export PATH=/opt/local/bin:/opt/local/sbin:$PATH 
.bash_history: export PATH=${PATH}:/bin 
.bash_profile: export PATH=/bin:$PATH 
.bashrc: export PATH=${PATH}:/bin:/usr/bin

Best Answer

Terminal.app correctly starting the shell does’t mean much: it runs /usr/bin/login (with the full path) by default, which invokes your default shell (again: defined with a full path) as an interactive login shell (which will in turn read both .profile and .bashrc and leave you with a working $PATH). Your problem is non-interactive shells, which do neither, do not get any $PATH settings. That seems to point to OS X’ default path settings having somehow been clobbered.

To check this, run cat /etc/paths. The output should (at the very least) be

/usr/bin
/bin
/usr/sbin
/sbin

(these are the defaults on a pristine OS X install). If the first two are missing, you have your cause – and an easy solution:

mv /etc/paths /etc/paths.old # if you want to keep the current contents
def_paths=(/usr/bin /bin /usr/sbin /sbin)
for p in ${def_paths[@]}; do echo $p >> /etc/paths; done
cat /etc/paths.old >> /etc/paths # append previous contents

Note that as /etc/paths is owned by root, you will have to sudo su for this to work.