Mac – bash file recovery

bashmacterminalunix

I think I messed up my bash file /bin/bash when trying to create my first script, it displays a message saying "process completed" and locks my terminal, if I switch to another shell e.g sh, then it's fine, is there a way to restore the contents of bash file or anything like that, i don't have a backup as I never managed to use time machine.
Thanks for the help

Best Answer

The problem likely is with one of the three following files all located at the root of your home directory: .bashrc, .profile, or .bash_profile. Try this:

1) While in an sh session, navigate to your home directory if you are not already there (type in cd ~).

2) Run the command ls -al and determine which of those files (.bashrc, .profile, or .bash_profile) are present in your home folder.

3) It is likely the file which is causing the problem is the last one you were editing or is linked to by one of the three previously named files. Either move or change the file name of one of those files in of your home directory. If you are not sure which file you were working with, I would start with .bashrc.

Example command to move .bashrc: mv .bashrc bashrc.bak

4) Once you have moved or changed the file name of one of the previously listed files, try opening a new bash session. If the session launches successfully, then you know which file needs to be edited further. If the bash session does not launch, restore the file back to its original location. If no one file appears to be the cause, try moving or changing the file names of all named files.

5) Make a backup of the file which is causing problems.

To resolve the "process completed" message, you will need to troubleshoot the script looking for return or exit statements which may be written incorrectly. If those are not present, you may have to go the route of commenting lines of the script out to determine where the problem is. To comment a line out, add a # character to the beginning of the line.

During this last step, you will need to move a copy of the offending file back to its original location. To make things easier, I would probably open the file using TextWrangler or from nano or vim in another sh session to make changes.

(Note: TextEdit and XCode will also work. However, to open the file, you will need to issue a command such as "open -a TextEdit.app ~/.bashrc" to open the file because the Open File Window does not allow for opening files whose names start with a period.)