Every command fails with "command not found" after changing .bash_profile?

  • I was updating my .bash_profile, and unfortunetly I made a few updates and now I am getting:

    env: bash: No such file or directory
    env: bash: No such file or directory
    env: bash: No such file or directory
    env: bash: No such file or directory
    env: bash: No such file or directory
    -bash: tar: command not found
    -bash: grep: command not found
    -bash: cat: command not found
    -bash: find: command not found
    -bash: dirname: command not found
    -bash: /preexec.sh.lib: No such file or directory
    -bash: preexec_install: command not found
    -bash: sed: command not found
    -bash: git: command not found
    

    My bash_profile actually pulls in other .sh files (sources them) so I am not exactly sure which modification may have caused this.

    Now if I even try and to a list of files, I get:

    >ls
    -bash: ls: command not found
    -bash: sed: command not found
    -bash: git: command not found
    

    Any tips on how to trace the source of the error, and how to be able to use the terminal for basic things like listing files etc?

    What happens if you use the full path like so: /bin/ls ? And what sorts of changes were you making?

    Yes, paste your .bash_profile

    /bin/ls lists the files, but then I see these 2 lines below it: `-bash: sed: command not found -bash: git: command not found`

    In case your problem was with `/etc/environment`, use the following command (in centos 6): `ssh -t [email protected] "/bin/bash -c '/bin/mv /etc/environment /tmp/'"`

  • January

    January Correct answer

    8 years ago

    It looks to me that at one point or another you are overwriting the default PATH environment variable. The type of errors you have, indicates that PATH does not contain /bin, where the above commands (including bash) reside.

    For example, if you do

    PATH=/home/user/bin
    

    instead of

    PATH="$PATH":/home/user/bin
    

    I did do this at one point: `export PATH=PATH:EC2_HOME/bin`

    You have to use '$' in the second PATH string - `export PATH=$PATH:EC2_HOME/bin`

    strange how 'ec2-describe-regions' doesn't work, even though it is in my path, yet this works: $EC2_HOME/bin/ec2-describe-regions I did a source reload also....

    doing a echo $PATH, I see it is repeated 3 times: `EC2_HOME/bin:EC2_HOME/bin:EC2_HOME/bin` how can I fix this, reboot? source'ing it doesn't seem to fix it.

    actually me running source seems to be adding a new one each time??

    Look, we don't know what you are doing, what other files you are sourcing, what is in them. And yes, every time you source the file, you *append* things to your path (which is fine, because normally .bash_profile will get read only once). Go through it one by one and see what happens. Also, isn't EC2_HOME a variable? If yes, then you should precede it with a `$` like this: `export PATH=$PATH:$EC2_HOME/bin`

    This was my problem. In order to reset my PATH so I could go into my bash profile and erase the bad PATH line in my `~/.bash_profile` I had to reset my PATH by doing: `PATH=/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin`

    I lose count of how much times I stucked into that problem and how much times this answer helped me a lot

    @Lauren omg thank you!

  • One way to begin debugging your bash script would be to start a subshell with the -x option:

    $ bash --login -x
    

    This will show you every command, and its arguments, which is executed when starting that shell.

    The --login option is specified because .bash_profile is read by login shells. Further information on debugging bash scripts can be found here: http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_02_03.html.

    Ultimately, I think that January's suggestion will work for you, but that link is worth a read for future problems.

    got this: -bash: bash: command not found

  • I may have found the problem. It worked for me, and it might work for you...

    I was defaulting with my editor to Windows (LF/CR) saves. Since I use both systems, it seemed logical. When I needed to mess with my .bash_profile, I realized after commenting out and tryig things that nothing worked. I changed my saves to OS X format (CR only) and voilà! No more "command not found" in the terminal!

    It may just be that easy!

    Very subtle thing that was driving me crazy. Thanks for adding this answer!

    This was what I was experiencing! Thanks. Quick tip is using this command to convert files on your unix environment: `$ dos2unix .bash_profile`

    Are you sure that's what you did? Since OS X is Unix it uses Unix-style line endings with a single linefeed (LF) character, not carriage return (CR). The only popular operating system that used the latter were OS 9 and prior and Linux/Ubuntu applications would be quite confused by it.

  • After I couldn't re-run . ~/.bash_profile or any usual commands like whoami, grep, etc. I figured a way to just re-export the required paths:

    export PATH=/usr/local/jdk/bin:/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/opt/cpanel/composer/bin:/usr/local/easy/bin:/usr/local/bin:/usr/X11R6/bin:/root/bin
    

    This should work in most systems, although some of these paths are not present in all Linux packages. It worked for me.

  • I think I may have found the answer to the problem if not for you then for others who have a similar problem. My answer to this is that I do not have a .bash_profile.

    So I was searching all over the web and I found the solution. Which is basically open the terminal, type touch ~/.bash_profile and press Enter. That fixed my problems. Hope it does the same for you

    LOL... -bash: touch: command not found

  • I have the same problem like you. I can't use many popular command (ls, vi/vim, ..) and can't edit /root/.bashrc when I log in with su into root.

    Finally. I found the solution for this problem. Just login root with command:

    su -m
    

    After that, you can use

    vim /root/.bashrc
    

    to edit PATH.

    Goodluck!

  • I had the same problem. You may have missed a $ while exporting PATH. You should open the .bash_profile in TextEdit. If you can't find the file in the directory, press Command + Shift + > to show hidden files.

    Then make correction to the PATH and then save.

    In the directory on Terminal, type: source .bash_profile.

    This should resolve the issue.

    Thanks it solved my problem, finally i was able to edit my bash_profile file.

  • I had exactly the same problem:

    If I put in lxterminal:

    set | grep "jerom/bash"
    PATH=/usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/home/jerom/bash/
    

    if I entered exactly the same command in tty2 I got:

    set | grep "jerom/bash"
    PATH=/usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/home/jerom/bash\r
    

    \r means DOS end of line, so I opened file ~/.bashrc and change the ends of lines in Krusader to unix style. And its working already!!! :-)

    The DOS EOLs make spourious problems also in bash scripts.

  • I got the same problem just now after I changed the .bash_profile. And I want to change it back but now I can't, because I lost all the commands, especially the nano command and the vim command, so I can't open the .bash_profile with command. And the .bash_profile is a hidden file I can't open it with the Finder.

    So I need to find a way to open the .bash_profile file. After I tried many ways I found that I can use Atom, because Atom can read all files in a project folder, include the hidden files. So just open Atom, and select the File -> Open, choose <yourusername> folder, and then, all hidden files is showing on the Atom's tree view, include the .bash_profile!! After I removed the last modification and reopen the Terminal, all commands came back! :)

License under CC-BY-SA with attribution


Content dated before 6/26/2020 9:53 AM