mkdir error in bash script

69,269

Solution 1

Change:

mkdir -p $deploydir

to

mkdir -p "$deployDir"

Like most Unix shells (maybe even all of them), Bourne (Again) Shell (sh/bash) is case-sensitive. The dir var is called deployDir (mixed-case) everywhere except for the mkdir command, where it is called deploydir (all lowercase). Since deploydir (all lowercase) is a considered distinct variable from deployDir (mixed-case) and deplydir (all lowercase) has never had a value assigned to it, the value of deploydir (all lowercase) is empty string ("").

Without the quotes (mkdir $deploydir), the line effectively becomes mkdir (just the command without the required operand), thus the error mkdir: missing operand.

With the quotes (mkdir "$deploydir"), the line effectively becomes mkdir "" (the command to make a directory with the illegal directory name of empty string), thus the error mkdir: cannot create directory'.

Using the form with quotes (mkdir "$deployDir") is recommended in case the target directory name includes spaces.

Solution 2

Change:

mkdir -p $deploydir

to

mkdir -p "$deploydir"

Solution 3

You can't have colons in file names on Windows, for obvious reasons.

Share:
69,269
Dónal
Author by

Dónal

I earn a living by editing text files. I can be contacted at: [email protected] You can find out about all the different kinds of text files I've edited at: My StackOverflow Careers profile

Updated on May 01, 2020

Comments

  • Dónal
    Dónal about 4 years

    The following is a fragment of a bash script that I'm running under cygwin on Windows:

    deployDir=/cygdrive/c/Temp/deploy
    
    timestamp=`date +%Y-%m-%d_%H:%M:%S`
    deployDir=${deployDir}/$timestamp
    
    if [ ! -d "$deployDir" ]; then
        echo "making dir $deployDir"
        mkdir -p $deployDir
    fi
    

    This produces output such as:

    making dir /cygdrive/c/Temp/deploy/2010-04-30_11:47:58
    mkdir: missing operand
    Try `mkdir --help' for more information.
    

    However, if I type /cygdrive/c/Temp/deploy/2010-04-30_11:47:58 on the command-line it succeeds, why does the same command not work in the script?

    Thanks, Don

  • SourceSeeker
    SourceSeeker about 14 years
    That's true. But who said anything about Windows?
  • unwind
    unwind about 14 years
    @Dennis: The question? "[...] running under cygwin on Windows"?
  • SourceSeeker
    SourceSeeker about 14 years
    Oops, back to kindergarten for me. However, Cygwin does support colons in filenames.
  • Ken Ingram
    Ken Ingram over 4 years
    I had thi sproblem and the fix was really simple. Remember not to leave spaces between variable "=" and assigned variable value.