Bash can't run command from script: mkdir command not found

7,047

Solution 1

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:

Solution 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.

Share:
7,047

Related videos on Youtube

NapoleonTheCake
Author by

NapoleonTheCake

Updated on September 18, 2022

Comments

  • NapoleonTheCake
    NapoleonTheCake almost 2 years

    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?