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?

  • 6
    echo "set -o noclobber" > ~/.bashrc (remember to add a second angle bracket)
    – hhaamu
    Nov 14, 2011 at 14:00
  • 3
    For next time: Tips for putting ~ under source control Nov 14, 2011 at 23:47
  • 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, 2011 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, 2012 at 0:09

2 Answers 2

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

  • 3
    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, 2011 at 19:54
  • 1
    set and declare (without arguments) display all of this at once. Nov 15, 2011 at 12:19
  • @enzotib, great idea. i did wonder where the default .bashrc would reside...
    – ixtmixilix
    Nov 15, 2011 at 22:27

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

  • The functions command doesn't exist for me on 4.2.10(2)-release. You probably mean declare -f.
    – Chris Down
    Nov 14, 2011 at 14:30
  • Actually, I meant "typeset -f" Nov 14, 2011 at 14:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.