20

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?

ixtmixilix
  • 13,230
  • 7
    echo "set -o noclobber" > ~/.bashrc (remember to add a second angle bracket) – hhaamu Nov 14 '11 at 14:00
  • 3
  • 1
    For the future: Backup your files so this isn't an issue. You might also consider creating a separate file for these quick add-ons and sourcing it somewhere like in .bashrc. Provides a basic safety net and you can easily turn it on and off as necessary. – Joe Nov 18 '11 at 23:59
  • 2
    You can save your current settings but if your .bashrc had any logic in it that depended on local variables like host, user, etc. that is probably unrecoverable. The real answer is to restore from your most recent backup. You do have a recent backup right? – jw013 Dec 19 '12 at 00:09

2 Answers2

24
  • 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.

manatwork
  • 31,277
  • 4
    I would also suggest to start from the default .bashrc for your system. In Debian it is in /etc/skel, or you can create a new user, if you can, to get a brand new .bashrc. – enzotib Nov 14 '11 at 19:54
  • 1
    set and declare (without arguments) display all of this at once. – rozcietrzewiacz Nov 15 '11 at 12:19
  • @enzotib, great idea. i did wonder where the default .bashrc would reside... – ixtmixilix Nov 15 '11 at 22:27
  • Be aware that declare -f >> ~/.bashrc could perhaps add A LOT of lines to your .bashrc. In my case that was 13216 lines because of git, npm and rvm combined. – Cadoiz Sep 19 '22 at 06:20
2

typeset -f > .bashrc should do it, providing all you had were aliases.

Paul Tomblin
  • 1,678