1

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

#!/bin/bash
PATH=/home/name/
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?

closed as off-topic by jasonwryan, techraf, Stephen Kitt, GAD3R, Thomas Dickey Nov 26 '16 at 17:08

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions describing a problem that can't be reproduced and seemingly went away on its own (or went away when a typo was fixed) are off-topic as they are unlikely to help future readers." – jasonwryan, Stephen Kitt, Thomas Dickey
If this question can be reworded to fit the rules in the help center, please edit the question.

6

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:

2

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.

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