Did I break bash? Getting "sudo" command not found...

Discussion in 'macOS' started by mar2194, Jan 9, 2013.

  1. mar2194 macrumors member

    mar2194

    Joined:
    Feb 22, 2010
    Location:
    Los Angeles, CA
    #1
    Hi guys and gals,

    I just installed GMT4 through MacPorts. I don't know why that would have affected my .bashrc file but I'm getting the following errors when running commands:
    "-bash: ls: command not found"
    "-bash: emacs: command not found"
    "-bash: sudo: command not found"

    I've managed to re-add these commands by typing
    "export PATH=$PATH:/usr/bin" but the result is only temporary. Can someone please tell me what file I need to add /usr/bin to in order to fix this permanently?
    After typing "export PATH=$PATH:/usr/bin" and running an echo of my path I get the following
    mar2194-3:etc mar2194$ echo $PATH
    /opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/usr/lib/gmt/bin:/usr/local/netcdf-4.1.1/bin:/bin:/usr/local/bin:/usr/bin
    mar2194-3:etc mar2194$

    Can someone help me out with this? I know it's not rocket science I just have no idea where to look.

    - mar2194
     
  2. mar2194, Jan 10, 2013
    Last edited: Jan 10, 2013

    mar2194 thread starter macrumors member

    mar2194

    Joined:
    Feb 22, 2010
    Location:
    Los Angeles, CA
    #2
    I think I fixed something...

    SORY FOR ALL THE CODE TAGS! I didn't know they would be formatted this way and there are too many for me to take out right now. I will remove some in the morning if it's too difficult for anyone to read. ~mar2194

    So I've added export PATH lines to the .bashrc .bash_profile and .profile files in my home directory for
    Code:
    /usr
    ,
    Code:
    /bin
    ,
    Code:
    /usr/bin
    ,
    Code:
    /usr/local/bin
    &
    Code:
    opt/local/lib/gmt4/bin
    . Sudo is working again however
    Code:
    ls
    is still not working (among many other commands). when I load the file
    Code:
    .bashrc
    or
    Code:
    .bash_profile
    (without logging out or anything) by typing
    Code:
    source ~/.bashrc
    or
    Code:
    source~/.bash_profile
    I get the
    Code:
    ls
    command back but when I open new terminal tabs I have to load
    Code:
    ~/.bashrc
    ,
    Code:
    .bash_profile
    or
    Code:
    .profile 
    manually to get ls functionality (and all other commands -
    Code:
    man
    doesn't work, etc). I thought those commands were in
    Code:
    /bin
    and most applications like
    Code:
    vi
    ,
    Code:
    emacs
    , etc are in
    Code:
    /usr/bin
    and
    Code:
    /bin
    . I've added all of those locations to the
    Code:
    .bashrc
    /
    Code:
    .bash_profile
    /
    Code:
    .profile
    files because when I load them manually using
    Code:
    export PATH
    at the beginning of a session, I get ls functionality back. Does anyone have a clue as how to fix this? I'm getting so frustrated I don't know where else to look for the problem.
     
  3. marsmissions macrumors 6502

    marsmissions

    Joined:
    Jan 5, 2010
    Location:
    Washington, US
    #3
    restore the modifications you made to your .bashrc or any similar file and it will work again. One of your paths is malformed or conflicting.

    By restore I mean delete everything you added.
     
  4. switon macrumors 6502a

    Joined:
    Sep 10, 2012
    #4
    RE: MacPorts, /opt, and PATH...

    Hi mar2194,

    MacPorts, by default, prepends all of those /opt/... directories to your PATH variable. As a consequence of the prepending, the OS looks in these directories first for any bash commands you wish to run. Thus if you had a "maxima" program in both your /opt/local/bin/ as well as your /usr/local/bin/ directories, then the one in /opt/local/bin/maxima would execute and not the one in /usr/local/bin/maxima.

    So you have two choices, either build the MacPorts's versions of the commands you wish to use and then they will be in the /opt/... directories and your current PATH will find them, or add the /bin, /usr/bin, /usr/local/bin, /sbin, /usr/sbin, /usr/local/sbin, /usr/texbin, etc. directories to your PATH variable and export your PATH variable. If you do this in your ~/.bashrc and ~/.bash_profile files, or in the system wide /etc/bashrc and /etc/profile files, and your PATH is exported from these files, then when you start a new Terminal window it will inherit the proper PATH variable and you won't have to redefine PATH and export it from every new Terminal window.

    Regards,
    Switon
     
  5. mar2194 thread starter macrumors member

    mar2194

    Joined:
    Feb 22, 2010
    Location:
    Los Angeles, CA
    #5
    Where is the PATH variable that I need to add to? How do I export it? I don't really know what that even means. Can someone just tell me what files and what lines I need to add to in order to restore access to basic commands (ls, etc)?
     
  6. switon macrumors 6502a

    Joined:
    Sep 10, 2012
    #6
    RE: PATH and ...

    Hi mar2194,

    The following bash command line should be added to your ~/.bashrc, ~/.bash_profile files where ~ is shorthand for your account login directory, that is, /Users/username. This line adds the directories where the standard Mac OS utilities are located. The PATH variable is the variable that bash checks when it tries to find a program. The individual program directories are separated by colons, and the PATH is read from left to right. Since your MacPorts prepended a bunch of MacPorts directories, the /opt/... ones, to the start of your regular PATH variable, the bash shell could not find its normal utilities. The "$PATH" tacked onto the end of the "PATH=..." define appends the old PATH's value to the ones you are adding. The "export PATH" command then exports the PATH variable, that is, it makes all child processes of the shell process (i.e., all subprocesses of the bash shell) know the new value of PATH.

    Code:
    PATH=/bin:/usr/bin:/usr/local/bin:/sbin:/usr/sbin:$PATH; export PATH
    
    Once you have added the above code line to your ~/.bashrc and ~/.bash_profile files, then reboot your computer and login to your account once again. You should then be able to use the "ls" and "sudo" commands.

    Regards,
    Switon
     
  7. switon macrumors 6502a

    Joined:
    Sep 10, 2012
    #7
    RE: those /opt/... directories in your PATH...

    Hi mar2194,

    I was wondering why the MacPorts broke your PATH in the first place. Did MacPorts not compile correctly on your machine? Did the MacPorts build fail?

    My MacPorts also prepended the /opt/... directories to my PATH, but this did not break my PATH and I still have access to the system utilities. (I did have a different problem, in the MacPorts built a utility with the same name as one I built myself, and thus the prepended /opt/... directories would run the MacPorts utility instead of mine, so I had to "fix" this by swapping the order around in my PATH variable, but simply building MacPorts should not have messed up your PATH so badly that you wouldn't have access to "ls" and "sudo". So, what is going on with your system?

    Switon
     

Share This Page