How do I restore .bashrc to its default?

Idea: There exist backup copies of .bashrc, .profile etc. in /etc/skel/. So one could replace a corrupt .bashrc simply by overwitting from there.

Caution: if you replace the .bashrc file with a fresh one, it will remove any other modification(s) you have made to it. For example, one could add aliases, custom function or PATH in .bashrc. When you replace the file, all those modifications will be lost. Better you can keep a copy of your modified .bashrc before replacing it. Later, you can carefully extract the required part from it. To keep a backup copy of your modified .bashrc in your home directory with name my_bashrc use the following in a terminal,

/bin/cp ~/.bashrc ~/my_bashrc

Why /bin/cp: In case if you have messed with your $PATH variable when changed ~/.bashrc all the executable will be unavailable from your terminal and cp will not work anymore. So it is recommended to call cp with its full path as /bin/cp while you are trying to copy something with corrupt ~/.bashrc.

Finally, use the following command in your terminal to replace the ~/.bashrc with a fresh copy,

/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 takes place immediately, write in the terminal,

. ~/.bashrc

or,

source ~/.bashrc

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


If you can't get a working shell

Via the file browser

  • Open the file browser, go to the home directory, press CtrlH so that hidden files are shown. Edit .bashrc as needed.
  • Open the file browser, go to /etc/skel, press CtrlH so that hidden files are shown. Copy .bashrc to your home folder to restore it to the default.

Via the run menu

  • Press AltF2, type gedit .bashrc, press Enter. Edit as needed.
  • Press AltF2, use the command /bin/cp /etc/skel/.bashrc ~/ as given in souravc's answer to restore it to the default.

Via the terminal

  • Open the terminal, and ignore that you don't have a shell. Go to EditPreferencesProfiles:

    enter image description here

  • Either create a new profile, or edit the current profile, to change the command:

    enter image description here

    Use either /bin/bash --norc or /bin/bash --rcfile=/etc/skel/.bashrc.

  • Start a new tab (with the custom profile, if you created one). Use the working shell as needed.
  • Delete the custom profile, or uncheck the custom command option if you edited the default profile, once you're done.

If you have SSH

Run SSH with a custom command, which should help you bypass the .bashrc:

ssh -t <host> dash

The dash shell (aka /bin/sh) is minimal, but sufficient for restoring the .bashrc.

If you can't get a GUI and don't have SSH

Boot into recovery mode (How do I boot into recovery mode?), which will get you a root shell. Look in /home for your user's home directory.


Via the TTY

You can enter the TTY by pressing ctrl+alt+f1 where you have to login with your user-name and your password. ctrl+alt+f7 will bring you back to your GUI later.

Now copy the existing raw .bashrc from /etc/skel to your home directory by

cp /etc/skel/.bashrc ~/

Maybe even bring the rest over if you're not sure if your .profile was changed too.

cp /etc/skel/.profile ~/

Now to get the .bashrc to have immediate effect you might want to source it with:

source ~/.bashrc

If you did replace your .profile as well you need to reboot to make it take effect.

Tags:

Bashrc