How do I restore my PATH in ~/.bashrc when basic c

2019-09-21 14:23发布

问题:

While installing a program on Ubuntu, I messed up paths in my ~/.bashrc. Now I cannot use Linux commands like ls, vim, etc. Therefore I cannot edit the ~/.bashrc file (it's a cluster). How can I fix my shell startup files?

回答1:

Specify a Fully-Qualified Path to Your Editor

Specifying an absolute path to your editor (e.g. vim, nano, or emacs) will generally enable you to fix up your shell startup files. For example, to edit and re-source your Bash resource file:

/usr/bin/nano ~/.bashrc &&
    . ~/.bashrc

Start a Clean Environment

If you just want to start a new shell without inheriting from your current environment or sourcing your current .profile or .bashrc file, you can do that. For example:

/usr/bin/env -i /bin/bash --noprofile --norc

You should then be able to rely on sane system defaults to edit your PATH or other settings before sourcing your shell's startup files manually or just execing a new shell with:

command exec /bin/bash

Override PATH in the Current Shell

You might also try overriding the PATH in your current shell with system defaults using the standard getconf utility. For example:

export PATH=$(command getconf PATH)

You can also set your PATH manually to a minimal sane default like:

export PATH=/usr/bin:/bin:/usr/sbin:/sbin

so you can continue to work on your issue.

Clusters and Distributed Systems

This aspect is likely outside the scope of a site about programming, especially since we don't have enough information about your systems. In general, if you messed up system-wide or clustered environment settings, you may need to look at values in /etc/profile, /etc/profile.d, /etc/skel, NFS shares, or other locations where your account or application PATH is being set.

If the problem is local to an account, then the tips I provided above will help. You might also consider logging into another account that doesn't exhibit the same problems.

Knowing where to fix a multi-system configuration issue, or knowing what your system- or user-specific PATH settings should be, is outside the scope of a reasonable Stack Overflow question. Short of that, though, any of the solutions above should work.



回答2:

If your $PATH is lost, no problem, you can find your editor in any of the usual places like /usr/bin/.

So you can start your edit with:

/usr/bin/vim .bashrc

You can much easier create simply a new one if you create a new user with your distribution and simply copy the .bashrc file from there to your own home directory. Each distribution provides a simple gui tool to add a new user to the system. So that should work without having a valid .bashrc.

What makes me wonder: All my PATH settings are not in .bashrc directly but in a system wide one. On fedora it is something like:

# Source global definitions
if [ -f /etc/bashrc ]; then
    . /etc/bashrc
fi

For ubuntu it will be different. But doesn't matter, because the creation of a new user will also help in this case ;)