So I stupidly inadvertently destroyed ~/.bashrc. If I have open terminals with the settings that were previously there, is there a way to export the current settings back to a new .bashrc? (I've tried set > ~/.bashrc
from one of said terminals with some measure of success, but wondering if there's some more magical way.)

- 67,283
- 35
- 116
- 255

- 273
2 Answers
One thing you can try is to recover your .bashrc
from the memory of a running instance of bash. On Linux, run gcore PID
to make a memory dump of a process specified by its PID. Whether this has a chance of working depends on how bash manages its memory; I haven't checked the source code to see if it's at all possible. It doesn't work for me on Debian jessie amd64.
If that doesn't work, you can save your current settings, but you can't recover the way they got set, so a lot of information will be lost. If you had configuration that depends on the machine, on the terminal type, etc. then you'll only recover the settings for whatever instances of bash are still running.
Print out all variables in a form that can be read back. This includes a lot of noise that you'll have to sort out. Environment variables (marked with
declare -x
) shouldn't be defined in your.bashrc
but you might have done so anyway. Remove variables that bash sets automatically (check the manual and look at the output ofdeclare -p
inbash --norc
).declare -p
Print out all functions. This includes functions not defined by you, for example functions defined by the completion system (for which you want
. /etc/bash_completion
instead).declare -f
Print out aliases. These can probably be used as they are.
alias
Print out shell options. Compare with the output of
shopt
inbash --norc
to see what you changed.shopt
Print out completion settings (if you use the context-sensitive completion system). Most of these probably come from the completion system; finding the ones you've tuned might be a little difficult.
complete
Print out key bindings, if you've defined key bindings in your
.bashrc
rather than in.inputrc
. This includes default bindings.bind -p
From now on, back up all your files, and put your configuration files under version control.

- 829,060
You can get the default .bashrc
from /etc/skel/.bashrc
.
Also there is a way to recover files opened by a certain process from /proc/PID/fd/<files>
, but it is not the case for .bashrc
as it is not permanently opened by the bash process.

- 3,752
- 2
- 13
- 28
-
I'm hoping to restore my real .bashrc (with all its magic), if possible. – cbmanica May 04 '16 at 17:22
-
1Well, you can print you modified environment variables with
echo
, your custom functions withdeclare
and aliases withalias
, this is some of the things i recall, other than that good luck. I am also curious whether there is a better way to 'recover' .bashrc . – magor May 04 '16 at 17:31 -
set
plusalias
seems to have restored most of what I had, but of course now the file is rather a disaster... – cbmanica May 04 '16 at 17:38
.bashrc
-- do one thing for interactive shells, other things for scripting shells. Or make settings depend on the type of terminal. – Barmar May 04 '16 at 17:13git
for example to save your customized settings files – magor May 04 '16 at 17:32