I've accidentally overwritten my ~/.bash_profile
file.
Does anyone have a "sample" one I could copy, or know where I might be able to find one?
Ask Different is a question and answer site for power users of Apple hardware and software. It only takes a minute to sign up.
Sign up to join this communityI've accidentally overwritten my ~/.bash_profile
file.
Does anyone have a "sample" one I could copy, or know where I might be able to find one?
If your terminal window is still open, you can save the current environment settings to rebuild. The recovered file will contain more information than you may have defined yourself, so remove the parts you don't need:
Defined vars and functions:
$ declare > .bash_profile.recovered
Aliases:
$ alias >> .bash_profile.recovered
There is a backup located at:
/etc/skel/.bash_profile
Just copy it over your current file.
.bash_profile
will not be reflected in it.
You can try to recover it using TestDisk.
If that doesn't work, type:
x56~:$ alias >> ~/.bash_profile
and
x56:~$ echo "PATH=$PATH" >> ~/.bash_profile
to at least recover your PATH and aliases, which you can paste in your new profile file.
Unfortunately this is what revision control is for. There is no way to undo modifications, while deletes through finder are actually moves to the trash.
Use TimeMachine in the future. Aside from this, it will also give you piece of mind when your current hard drive dies. I know people facing this problem now and if they only invested $30-$50 into a backup hdd, the problems would not happen. I recommend you just learn from this and back up using timemachine periodically.
Github as mentioned by Ian C is a great place to back your dotfiles, also you can find dotfiles from others'. Version control to the rescue here, don't know how many times it saved me, but definitely more than I can remember. The advantage of TimeMachine is that it also does version control, problem is that its stored by snapshot time, not logical version.
Good luck.
If you can't use Terminal to access your ~/.bash_profile
(e.g. it's corrupted) you can use BBEdit or any other GUI editor that can open files from a location and open your corrupted file in order to fix it.
BTW. if you know the location and name of any hidden file, this method can be used to open it (assuming it is a text based file).
This method works only if you saved the bash_profile
file to a point where you want to go back in TextEdit, doesn't work if edited using vi or nano (thanks to @nohillside for pointing this out). You can open the file using TextEdit (for ex: open ~/.bash_profile
) and click on File -> Revert To -> Browse All Versions...
and then select the version you want to revert back to.
vi
or nano
will not be versioned).
I found this website that has two example of the files of bash_profile and bashrc.