Can I recover my .bash_profile from an open Terminal tab?

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


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

Defined vars and functions:

$ declare > definitions.bash.txt

Aliases:

$ alias > aliases.bash.txt

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.