user3181113
user3181113

Reputation: 758

Bash: Invalidated commands

I've incurred a worrisome issue with my bash shell. I was editing my bash_profile and accidentally exported an incomplete command (export PATH=/usr/local/bin). After I had reloaded my terminal, nearly all of my bash commands fail to work properly. When I try to run any one of them, the errors state: command not found.

How do I fix this? Is there an alternate way to open or find my bash_profile?

I would appreciate any immediate input I can get on this issue. Thank you in advance.

Upvotes: 0

Views: 413

Answers (2)

mklement0
mklement0

Reputation: 437823

@fedorqui's comment provides a quick fix.

The OP could also have used the following to quickly get to a shell with default values for $PATH:

To create a bash shell with a pristine default environment:

  • without running profile/initialization scripts
  • without inheriting any environment variables from the current shell

run:

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

Note:

  • Due to use of env's -i option, many environment variables that are normally set will NOT be set in the resulting shell , such as USER, HOME and LANG.
    • Similarly, the $PATH value you'll get is presumably one hard-coded into bash itself, but it should provide access to at least the standard utilities.
  • --norc suppresses loading of ~/.bashrc, which normally happens by default for interactive non-login bash shells (bash also supports the --noprofile option to suppress loading of /etc/profile and ~/.bash_profile, but it doesn't apply here, since the shell created is a non-login shell).
  • If env is in the current shell's $PATH, env -i bash --norc will do.
  • env is in /usr/bin/ on at least Linux and on FreeBSD/OSX, probably also on other platforms.

Upvotes: 1

David W.
David W.

Reputation: 107040

You can execute commands if you can give the directory name. Almost all the basic Unix commands are under the /bin or /usr/bin directory. For example, /bin/mv.

Fortunately, builtin commands are still recognizable.

Move your .bash_profile and .bashrc file out of the way for now, and see what the system default is.

You can manually edit your PATH on the command line to:

$ PATH="/bin:/usr/bin"
$ cd
$ mv .bash_profile .bash_profile.bak
$ mv .bashrc .bashrc.bak
$ mv .profile .profile.bak
$ mv .bash_login .bash_login.bak

NOTE: Some of these mv command may fail simply because that particular file may not exist.

which will give you access to most of the basic Unix commands. Or you can specify the commands with their full directory names:

$ PATH="/bin:/usr/bin"
$ cd
$ /bin/mv .bash_profile .bash_profile.bak
$ /bin/mv .bashrc .bashrc.bak
$ /bin/mv .profile .profile.bak
$ /bin/mv .bash_login .bash_login.bak

Now, log in again and see what your default $PATH is set to. This is set by the /etc/profile. You might find that's just fine, and remove setting PATH in your startup script.

The standard for PATH is something like this:

  • /usr/share/bin or /usr/local/bin - These contain non-standard Unix/Linux commands. For example, if you install Maven on your system, the mvn command will usually be located in one of these directories (maybe as a symbolic link). This directory is a place where commands not found in the /bin and /usr/bin directory are stored. This directory is first, so you can replace the version which came with your system with more recent versions. For example, I might have VIM 6.4 installed, but I want to use version 7.3 instead.
  • /bin:/usr/bin - The standard directories where 99% of the Unix commands live.
  • $HOME/bin - These are executables you wrote -- either scripts or binaries. This is at the end of the PATH list because it makes sure that you don't accidentally execute the wrong version of the command. Imagine if some joker wrote a shell script called cp that executed /bin/rm instead and placed it in your $HOME/bin directory.

Other directories you'll see may include /sbin/ and /usr/sbin which are administrator commands (ping and ifconfig are sometimes in one of these directories.) /opt/bin/X11 (or wherever the X11 binaries are stored). Sometimes other commands will futz around with your PATH, for example Perlbrew.

Upvotes: 2

Related Questions