19

I am on a MacBook Pro, running the latest version of Mountain Lion.

I really need some help, I have managed some how to damage my .bash_profile (I think) so that every time I open up the terminal I get the error listed below.

-bash: export: `/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/local/go/bin': not a valid identifier
-bash: export: `/Users/rob/Applications/sbt/bin:': not a valid identifier
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

I am not sure what has happened, I have no sudo, cd or any normal commands. The only way I have been able to get to any of the main directories is through the go to folder command in finder and try to find the file to no avail.

To top it all off I think I created a file that might be causing the issue, I wanted to edit the .bash_profile so I typed

sudo nano ./bash_profile

This open a new file in nano which I think was then saved. After this I opened the real .bash_profile to add in the path for node.js.

If I can get to the .bash_profile I think I can get it back on track but I can't find it, should I reinstall bash? If so how would I do that on a mac, I tried using

brew install bash

to which I get

-bash: brew: command not found
6
  • First thing I would try is change to a different shell to be able to sort out the problem: see here on how to do that support.apple.com/kb/TA27005. This should enable you to modify / remove the offending file and change the shell back to bash.
    – Gjallar
    Nov 2, 2012 at 9:52
  • Thank you, I have tried reinstalling bash but the only thing that has changed is that -bash: export: '/Users/rob/Applications/sbt/bin:': not a valid identifier has gone. The one thing I am struggling with is were the first line might be stored, I have looked in my .bash_profile, .profile and .bashrc but nothing.
    – Strangeone
    Nov 2, 2012 at 11:16
  • Ok so I added /usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/local/go/bin to my .bash_profile and it seems to have fixed bash but I still get this error -bash: export: '/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/local/go/bin': not a valid identifier Any ideas?
    – Strangeone
    Nov 2, 2012 at 11:24
  • Can you try the following: PATH="/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/local/go/bin:$PATH" # Make sure to use double quotes not single quotes And on a new line export PATH.
    – Gjallar
    Nov 2, 2012 at 11:32
  • Thank you, that seems to have worked. Please can I just ask why you thought that would work?
    – Strangeone
    Nov 2, 2012 at 11:45

6 Answers 6

12

Just putting all my comments together for an answer:

First thing you should do is change the shell, this way you can set a shell that will not load the bash init-scripts (.bashrc, .bash_profile) - how to do this for the Mac OS X terminal app can be seen here: Apple Support

Now you should be able to open a terminal again and use your favourite command-line editor to open the .bash_profile file (e.g. nano or vi):

In this file you have to reset your PATH variable that is used by the terminal to find the programs it can execute.

This can be done with the following two lines:

PATH="/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:$PATH" # Make sure to use double quotes not single quotes And on a new line 
export PATH

This will first set the directories that you want to have on your PATH and the export this PATH to make it available to all programs started from this shell (via export).

24

⇧⌘. shows hidden files in file dialogs. You could try opening ~/.bash_profile with TextEdit and making it blank temporarily.

2
  • 1
    +1 for the useful keyboard shortcut I didn't know :)
    – Michel
    Apr 22, 2013 at 14:57
  • note: .bash_profile is at you home directory (Users/[username]/.bash_profile
    – Amos
    Oct 11, 2018 at 5:10
5

I was running on the same problem, and I just found simpler solution:

/usr/bin/nano .bash_profile

delete all your mess, then save the file

2
  • Best answer. Easy and guaranteed to work.
    – nikk wong
    Mar 17, 2017 at 20:40
  • This answer helped me figure out what to do. In my case, I did this and it didn't let me save it after modifying it, so I had to add /usr/bin/sudo in front of it
    – Splendonia
    Mar 18, 2022 at 9:47
3

I know this is an old thread but just wanted to leave here my solution in the hope someone else happens to have the same issue.

When I ran terminal from MacOS it was ok with my own user, but when changed to sudo it couldn't work (command not found).

I ended up fixing it with:

nano ~/.bashrc

Just removed the path that was causing the error. Restarted the terminal and everything is working fine now.

0

I discovered recently that DOS style files cause problems for both .bash* scripts in OS-X. I have to use DOS file endings for some code shared with Windows users, so if I edit .bash_profile while in that git repo I get lots of error messages when running the script (at login). The utility dos2unix fixed it.

-1

In addition to the previously mentioned showing of hidden files and using a text editor, there is also an easier way to open a new shell without changing your default shell. You could alt-click the Terminal app and choose "New Command...". Then type "sh" into the dialog and run that. This would open a non-bash shell and you could edit the file there. In the future it would be a good idea to keep a shell open or test running the new .bash_profile prior to exiting.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .