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?

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

2 Answers 2

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

4
  • 5
    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
    Commented Nov 14, 2011 at 19:54
  • 1
    set and declare (without arguments) display all of this at once. Commented Nov 15, 2011 at 12:19
  • 1
    @enzotib, great idea. i did wonder where the default .bashrc would reside...
    – ixtmixilix
    Commented Nov 15, 2011 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
    Commented Sep 19, 2022 at 6:20
2

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

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .