nullByteMe
nullByteMe

Reputation: 6391

Shell variable issue when trying to mkdir

Any ideas what is wrong with this code?

CLIENT_BUILD_DIR="~/Desktop/TempDir/"

if [ ! -d $CLIENT_BUILD_DIR ]
then
   {
      mkdir $CLIENT_BUILD_DIR
   }
fi

I get the error: mkdir: ~/Desktop: No such file or directory.

Obviously the directory is there and the script works if I replace the variable with ~/Desktop/TempDir/

Upvotes: 20

Views: 45426

Answers (3)

Oscar Montoya
Oscar Montoya

Reputation: 671

mkdir ${CLIENT_BUILD_DIR} will do. No directory will be created if it already exists.

Upvotes: 1

tomahh
tomahh

Reputation: 13651

The ~ character is not reinterpret when used in a variable.

You can use CLIENT_BUILD_DIR="$HOME/Desktop/TempDir/" instead.

Upvotes: 16

BeniBela
BeniBela

Reputation: 16917

The quotes prevent the expansion of ~.

Use:

CLIENT_BUILD_DIR=~/Desktop/TempDir/

if [ ! -d "$CLIENT_BUILD_DIR" ]
then mkdir "$CLIENT_BUILD_DIR"
fi

Upvotes: 30

Related Questions