undoing changes to bashrc from terminal

后端 未结 5 1428
半阙折子戏
半阙折子戏 2021-01-12 11:03

Unfortunately I just overwrote my .bashrc with

echo \"command\" > ~/.bashrc

as opposed to append

相关标签:
5条回答
  • 2021-01-12 11:55

    Sharing the link to an answer in another post which helped me. https://stackoverflow.com/a/40214165/13151296 If you only added a few touches to your bashrc, you can use this to get a fresh default bashrc and then add what you need using methods in previous answers of this thread.

    Type the following in your terminal,

    /bin/cp /etc/skel/.bashrc ~/

    It will replace your corrupt ~/.bashrc with a fresh one. After that you need to source the ~/.bashrc so that the change take place immediately, write in terminal,

    source ~/.bashrc

    or if that does not work you can close the terminal and open it again.

    0 讨论(0)
  • 2021-01-12 12:05

    Assuming you built your .bashrc with a bunch of similar echo commands and you just want to retrieve them:

    Is there a location where a session's commands are stored?

    If you run history, you should be able to retrieve the previous commands. For more information, see man history.

    In your case, you might find the output of history | grep bashrc useful.

    0 讨论(0)
  • 2021-01-12 12:08

    You can't completely recover. But you can partially recover using set.

    If you run set on the same terminal you'll be able to get a whole list of custom scripts and other environment variables set. And in that, it's upto to differentiate the ones that were part of .bashrc and others typed on that terminal. But you won't be able to recover the commands which used to be executed as part of bash login.

    0 讨论(0)
  • 2021-01-12 12:09

    For the future you may consider using a version control system such as git or hg in order to save previous versions of files such as ~/.bashrc. Then if you happen to do a > rather than >> in the future you should be able to recover the file back to the point the last time you commited it to the version control.

    An example of how to set this up for git would be:

    cd ~
    git init
    git add ~/.bashrc
    git commit -m "Added .bashrc to version control"
    # Time goes by...
    echo "export FOO=bar" >> ~/.bashrc # Added a new line
    git commit -am "Added FOO to .bashrc"
    # Time goes by...
    echo "export SHEEP=lambs" > ~/.bashrc # Eeek! We've overwritten our file
    # Version control to the rescue
    git checkout ~/.bashrc # file is restored
    echo "export SHEEP=lambs" >> ~/.bashrc # Done correctly this time!
    git commit -am "Added SHEEP to .bashrc"
    
    0 讨论(0)
  • 2021-01-12 12:09

    Cannot be recovered.

    I suggest you to type env in your terminal to view the most important environment variables (like LD_LIBRARY_PATH, PATH, PYTHONPATH, CLASS_PATH, JAVA_HOME, and so on ...) and add them back into a new .bashrc file.

    You can start from a copy of /etc/skel/.bashrc as a minimal file.

    0 讨论(0)
提交回复
热议问题