I have separate scripts for tasks in bash. Here is the broken one:

mkdir $PATH
cd $PATH && echo "done."
exit 0

Today it broke and first time it simply didn't want to run cd, but created directory. Second time it just said "mkdir command not found." Running this commands exactly with semicolon works fine. What the case?


PATH is an environment variable. It's what your shell uses to find the commands it is going to run. More precisely, the PATH environment variable contains a colon-separated list of directory names, which are searched in sequence for an executable with the name that you specify when you type a command. (Unless of course the command you type is a shell builtin, alias or function.)

When you set PATH in your script you are "masking" the value of the environment variable with the shell variable of the same name.

The takeaway from this is don't use all caps names for regular shell variables.

Since you don't intend an environment variable, just use a lowercase variable name.

Also see:

|improve this answer|||||

In bash, PATH is a special variable that tells bash where to look for installed programs. Unless you have a mkdir binary in /home/name/, it should output mkdir: command not found after PATH has been set. Use a different variable name to fix this problem.

|improve this answer|||||

Not the answer you're looking for? Browse other questions tagged or ask your own question.