Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question
5  
echo "set -o noclobber" > ~/.bashrc (remember to add a second angle bracket) –  hhaamu Nov 14 '11 at 14:00
2  
For next time: Tips for putting ~ under source control –  Gilles Nov 14 '11 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 '11 at 23:59
    
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 0:09

2 Answers 2

up vote 14 down vote accepted
  • 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.

share|improve this answer
1  
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

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

share|improve this answer
    
The functions command doesn't exist for me on 4.2.10(2)-release. You probably mean declare -f. –  Chris Down Nov 14 '11 at 14:30
    
Actually, I meant "typeset -f" –  Paul Tomblin Nov 14 '11 at 14:55

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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