Your shell stores a variety of key-value pairs called environment variables. Your PATH is just one example of one of these environment variables.
Without a PATH variable set, you would need to manually set the full path of every program you execute. Eg, you couldn't just run date
, you would need to run /usr/bin/date
every time. And if you later install a custom version of Date in /usr/local/bin/
, you would need to run it as /usr/local/bin/date
.
While inconvenient, this isn't impossible, so you see PATH is actually optional. What the PATH is for, therefore, is to simply automatically detecting locations for programs.
Since programs can reside in multiple places across the system, the PATH isn't just one directory. Instead, it is a colon-separated list of directories. When you execute a command in your shell, the shell searches these directories in order to try and find the command you want to run.
Going back to the date
example, you could set PATH=/usr/local/bin:/usr/bin
if you prefer to prioritise custom programs under /usr/local/bin
, with fallback to /usr/bin
if it's not in /usr/local/bin
. Then when you run date
, the shell will see /usr/local/bin/
at the front of the PATH list, so it will find /usr/local/bin/date
. If you deleted /usr/local/bin/date
, the shell will still find /usr/bin/date
, since /usr/bin/
is second in the PATH.
When you run alias testCMD=PATH=$PATH
, you get an alias which probably looks a bit like this:
testCMD='PATH=.:/usr/local/bin:/usr/bin:/bin'
Therefore when you run testCMD vim
, what the shell see is,
PATH=.:/usr/local/bin:/usr/bin:/bin vim
This just gives a one-off directive telling the shell where to look for vim
. If you just ran vim
by itself, you would get the same result, because your testCMD alias isn't changing the value of PATH.
Here is the difference if you do change the PATH:
$ PATH=/foo vim
bash: vim: command not found
Because the shell tried to look in some directory called /foo
and there was definitely no vim
in there.
Going on to the examples you gave where it wasn't working, it's because you're not doing a variable assignment. There is a difference between PATH=/foo vim
and PATH vim
. In PATH vim
, the shell thinks you're trying to find a program called PATH
. The shell will then look in every directory in $PATH
looking for a program called PATH
. Since you (probably) don't have a PATH command in any of those directories, you would get,
$ PATH vim
bash: PATH: command not found
The same principle holds when prefixing vim
with just the value of $PATH
: there is no assignment, so the shell thinks $PATH
is intended to be the name of some program. This is why you get:
$ $PATH vim
bash: .:/usr/local/bin:/usr/bin:/bin: No such file or directory
Overall, you usually just need to set PATH once in your ~/.bashrc
or ~/.bash_profile
. The exception would be if you're switching around between a variety of toolchains. This is actually what Python virtualenvs are for, to automate reconfiguring the PATH. Environment module systems such LMod are another powerful way of manipulating the PATH when required.
PATH vim
and$PATH vim
; this looks like you are taking fragments of valid commands and expecting them to be meaningful.