Bash – .bashrc overwritten but still sourced — how can it be recovered

bashbashrcdata-recovery

Usually when I find a command I want to alias, I echo it to my .bashrc like so:

[up button pressed to last command, then line edited so that it reads]
$echo "command-i-just-did" >> ~/.bashrc

There may be a better way to do this. But anyway, just now I overwrote the entire .rc file by using a single chevron. However, since the .bashrc is still current, it's still accepting my old aliases (for now of course). So is there a way to recover it?

Best Answer

  • alias without parameter outputs the definitions of currently defined aliases.
  • declare -f outputs the definitions of currently defined functions.
  • export -p outputs the definitions of currently defined variables.

All those commands output definitions ready to be reused, you can redirect their outputs directly to a new ~/.bashrc.

All lists will contain a lot of elements defined elsewhere, for example /etc/profile and /etc/bash_completion. So you will have to clean up the list manually.

Related Question