5

I'm actually using a Macbook Pro, and am quite new to non-Windows OS' (I know, for shame!), and I've made the mistake of overwriting my bash profile, instead of appending to it...

What I do have is a couple of open tabs in the Terminal, and am hoping that there might be a way of recovering my .bash_profile from this (or any way really).

  • Restore from backup. – Cyrus Apr 3 '15 at 19:47
  • Unless it has set -x at the beginning, so that it echoes all the statements as it's executing them, where in the terminal output do you expect to copy it from? And even that wouldn't have the commands from if and else clauses that don't get executed. – Barmar Apr 3 '15 at 19:53
  • Don't you have Time Machine enabled for backups? – Barmar Apr 3 '15 at 19:53
  • I don't have Timemachine switched on unfortunately, unless there is another way to recover? – Fifer Sheep Apr 3 '15 at 19:54
  • 1
    If you've not edited it previously perhaps you can create a new user account and copy it from there. Or from your equivalent of /etc/skel – roaima Apr 3 '15 at 22:57
4

If your terminal is still open, type env: it will display all your environment variables. If it's a fresh install or if you never made any change, the most important variables are PATH (this one always exist) and LD_LIBRARY_PATH (may not exists, I'm not used to osx).

For instance:

$ env
...
PATH=/usr/bin:/bin:/home/user/bin:/sbin
...

It's also a common thing to source .bashrc in that file.

Then you can edit your .bash_profile to add a line like this:

# Source your .bashrc
if [ -f ~/.bashrc ]; then
  source ~/.bashrc
fi

# Export the interesting env variables you
#   displayed from env command previously
export PATH=/usr/bin:/bin:/home/user/bin:/sbin

It's not a real solution since it's not a real "backup", but keep in mind that as long your terminal is open, you can know your environment variables, and main purpose of .bash_profile is to set those variables.

Also, you may have some aliases: in your opened terminal, type alias to display all the defined aliases. You can copy and paste the ouput as-is in your .bash_profile. (obviously, if those aliases are available in your .bashrc you don't need to define them in your .bash_profile since you source the .bashrc previously.)

  • Good call. Although this does not do what the OP asked for, it may provide enough info for him to reconstruct his file. – Warren Young Apr 4 '15 at 0:12
  • Hero! Using a combination of env, alias and type function_name for each function - I was able reconstruct most of the profile. At least that which I care most about anyway. – Fifer Sheep Apr 4 '15 at 1:42
3

This just happened to me and I recovered as follows...

Defined vars and functions:

$ declare > definitions.bash.txt

Aliases:

$ alias > aliases.bash.txt
2

You seem to be implicitly asking if there is a place in RAM where the contents of this file are stored. The answer is, "no." The file is simply read and executed during shell startup, then discarded. No Unix shell needs continuous access to its startup script(s).

Some of the file's contents are still retrievable, per apaul's answer.

There is a tiny chance that there are fragments of the file itself in freed RAM that hasn't yet been reallocated or returned to the OS, but it's probably not worth your time to try to construct a program to dump the contents of RAM to a disk file and then plow through it to try and find your file contents.

Take the lessons you've been given and learn from them:

  1. Think twice before pressing Enter on a command involving > instead of >>, just as you have hopefully learned to be careful with rm commands.

  2. Set up Time Machine, soonest.

    Once upon a time, not having a recent backup was a sad but too-common tale. Now it's unforgivable. Apple has gone and made it as easy as you could reasonably ask.

  • Yep, it's exactly what I was asking. I had a feeling that this was the case - and have most certainly learned some valuable lessons! Thank you for confirming, and I will mark this as the answer. – Fifer Sheep Apr 3 '15 at 21:28
  • I'm somewhat ashamed to admit Richard that I'm studying Software Engineering, and know git very well :( – Fifer Sheep Apr 3 '15 at 22:06
0

A stupid script did that to my bashrc so I ran the following to retrieve most of my file.

# Retrieve functions definitions
$ declare > .bashrc.new
# Retrieve aliases
$ alias >> .bashrc.new
# Retrieve env
$ echo "export $(env | grep ^PATH=)" >> .bashrc.new

Here, most of your .bashrc is almost reconstructed. Now you just need to make small adjustements to ensure it matches your best practices. ;)

A cool command to find the defintion of your custom functions: type my_function >> .bashrc.new.

Also, make sure to review your env return value to include what you think is important into the newly created file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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