Accidentally removed all commands in terminal

I was following a tutorial and put in the following line:

export PATH="~/.composer/vendor/bin/"

Now no commands work at all, not even things like ls.

What does the command do that I copy and pasted in and how do I fix it?


What you did

$PATH is the variable that tells your shell (bash by default) where to look for your commands. It contains a list of directories, separated with colons (:). When you type ls, the shell will search all the directories listed within $PATH (/bin, /usr/bin, and so on) until it finds a file called ls, and will execute that.

Normally, you set a variable simply with

variable="foo bar baz"

...however, that only sets that variable for the current shell -- those changes won't carry over to subshells, which can be important in (for example) scripts. Using

export variable="foo bar baz"

...will allow you to use the variable in subshells.

So, when you use export PATH="~/.composer/vendor/bin/", you are overwriting the $PATH variable -- so when you try to use ls, the shell looks in ~/.composer/vendor/bin/, doesn't find any file named ls, and so fails. What you probably want to do is add ~/.composer/vendor/bin/ to your $PATH, which you can do with:

export PATH="$PATH:~/.composer/vendor/bin/"

You can view the current contents of your $PATH with echo $PATH.

How to fix it

All of this only applies to the current shell (and all its subshells, since you used export), so you can fix the problem most simply by opening a new terminal, as Mateusz Szlosek's answer notes.

If you do want to alter your $PATH permanently, you can put that line at the end of your ~/.bashrc, which is sourced whenever you open an interactive shell. This is standard practice if you want to write a few of your own scripts (on Linux, we use ~/bin for user scripts, but they can go anywhere).


export command works in the current session only. You can "fix" it by opening new Terminal window/tab.