59

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?

5
  • What happens if you use the full path like so: /bin/ls ? And what sorts of changes were you making? Sep 12, 2012 at 2:24
  • 2
    Yes, paste your .bash_profile
    – January
    Sep 12, 2012 at 2:32
  • /bin/ls lists the files, but then I see these 2 lines below it: -bash: sed: command not found -bash: git: command not found
    – Blankman
    Sep 12, 2012 at 2:40
  • In case your problem was with /etc/environment, use the following command (in centos 6): ssh -t root@<server-ip> "/bin/bash -c '/bin/mv /etc/environment /tmp/'"
    – Edenshaw
    Apr 17, 2019 at 13:19
  • For VM, after reboot, it goes to "Entering emergency mode. Exit the shell to continue.", enter xfs_repair -v -L /dev/dm-0 and then reboot seems fix the issue.
    – Ivan Chau
    Jul 12, 2021 at 3:59

9 Answers 9

76

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
10
  • I did do this at one point: export PATH=PATH:EC2_HOME/bin
    – Blankman
    Sep 12, 2012 at 2:45
  • 2
    You have to use '$' in the second PATH string - export PATH=$PATH:EC2_HOME/bin Sep 12, 2012 at 2:53
  • 2
    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
    – January
    Sep 12, 2012 at 3:07
  • 3
    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
    – Lauren
    Aug 8, 2018 at 22:01
  • 1
    I lose count of how much times I stucked into that problem and how much times this answer helped me a lot
    – IC_
    Nov 20, 2018 at 13:32
25

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.

2
  • 1
    got this: -bash: bash: command not found Sep 22, 2019 at 12:12
  • Fantastic solution. Easy and simple for spotting just where the error is. Thanks! But there seems to be a "problem". After I use the commands, any executed command is displayed in the console. Is there a way to turn it off other than restarting the shell? I'm not really versed in bash.
    – carloswm85
    Oct 22, 2021 at 0:53
13

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!

3
  • Very subtle thing that was driving me crazy. Thanks for adding this answer! May 7, 2014 at 20:51
  • 1
    This was what I was experiencing! Thanks. Quick tip is using this command to convert files on your unix environment: $ dos2unix .bash_profile
    – melwil
    Dec 2, 2016 at 8:43
  • 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. Apr 30, 2018 at 9:02
6

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.

4

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

1
  • 9
    LOL... -bash: touch: command not found
    – Kumar KL
    May 5, 2016 at 7:11
2

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!

2

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.

1
  • Thanks it solved my problem, finally i was able to edit my bash_profile file. Sep 2, 2019 at 5:57
1

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.

1

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! :)

Not the answer you're looking for? Browse other questions tagged or ask your own question.