10

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?

  • 1
    A good time to mention that GitHub is an excellent place to store your dotfile customizations. For an example of how to do this see: sursolid.com/managing-home-dotfiles-with-git-and-github – Ian C. Sep 26 '11 at 13:19
  • 2
    Tip: Use Time Machine to make regular backups of your files so you can recover them if accidentally deleted or edited. The single best investment you can make in your computer is a backup plan. – Chris Page Sep 26 '11 at 14:43
  • Just FYI, I don't think os x has a "default" .bash_profile. – Robert S Ciaccio Sep 26 '11 at 20:32
  • 3
    This really could have been answered by google ("sample bash_profile"). The 3rd hit provides a rather massively complete sample: tldp.org/LDP/abs/html/sample-bashrc.html – user10355 Sep 26 '11 at 21:28
6

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
3

Search for dot_files in Google or Github if you like a sample one.

If you like to restore it, you could try to get it from your TimeMachine backup (if present)

  • 2
    or any other backup - if no backup start producing one now before doing anything else – user151019 Sep 26 '11 at 12:41
2

There is a backup located at:

/etc/skel/.bash_profile

Just copy it over your current file.

  • Might be worth to add that this is the standard template, so any changes the user made to his local .bash_profile will not be reflected in it. – nohillside Feb 18 '14 at 12:51
  • 3
    As of macOS Yosemite (10.10) this appears to no longer be true. It is the standard arrangement on other Unices, but I can no longer find the skeleton file for new users in the file system. – tripleee Oct 5 '16 at 3:49
2

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.

1

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.

  • Time Machine just saved my day. I second this strategy, as someone who likes gnarly command-line aliases that sometimes are missing a backslash and ruin everything. – Alien Life Form Feb 15 '18 at 20:29
0

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).

0

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.

An image displaying the above procedure

  • 2
    Might be work mentioning that this only works for changes applied with TextEdit (changes applied with vi or nano will not be versioned). – nohillside Jul 3 at 7:22

You must log in to answer this question.

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