How do I restore .bashrc to its default? [duplicate]

I've been trying to install Android developer tools on Eclipse, and I followed the information in this video.

Like the video said, I added the following two lines of code to the .bashrc file:

export PATH=$(PATH):-/android-sdk/tools/
export PATH=$(PATH):-/android-sdk/platform-tools/

It seems, however, that this did not help me to install the Android files on Eclipse, as I am still having problems with that. What's more, it seems that I can't make any command in the terminal without getting a major error now! Every time I try to make a command in the terminal I get the following message:

The command could not be located because '/usr/bin' is not included in the PATH environment variable.

Is there away to restore .bashrc to its default?


Solution 1:

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.

Solution 2:

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.