Can I recover my .bash_profile from an open Terminal tab?
Solution 1
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.)
Solution 2
This just happened to me and I recovered as follows...
Defined vars and functions:
$ declare > definitions.bash.txt
Aliases:
$ alias > aliases.bash.txt
Solution 3
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:
-
Think twice before pressing Enter on a command involving
>
instead of>>
, just as you have hopefully learned to be careful withrm
commands. -
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.
Solution 4
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.
Related videos on Youtube
Fifer Sheep
Updated on September 18, 2022Comments
-
Fifer Sheep almost 2 years
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).
-
Barmar about 9 yearsUnless 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 fromif
andelse
clauses that don't get executed. -
Barmar about 9 yearsDon't you have Time Machine enabled for backups?
-
Fifer Sheep about 9 yearsI don't have Timemachine switched on unfortunately, unless there is another way to recover?
-
roaima about 9 yearsIf 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
-
-
Fifer Sheep about 9 yearsYep, 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.
-
Warren Young about 9 yearsGood call. Although this does not do what the OP asked for, it may provide enough info for him to reconstruct his file.