Shell variable issue when trying 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: there is no such file or directory.

Obviously, the directory exists and the script works if I replace the variable with ~ / Desktop / TempDir /

+6
source share
3 answers

Quotes prevent expansion ~.

Using:

 CLIENT_BUILD_DIR=~/Desktop/TempDir/ if [ ! -d "$CLIENT_BUILD_DIR" ] then mkdir "$CLIENT_BUILD_DIR" fi 
+14
source

Why not just:

 mkdir -p "$CLIENT_BUILD_DIR" 

Note -p .

+6
source

The ~ character ~ not interpreted when used in a variable.

Instead, you can use CLIENT_BUILD_DIR="$HOME/Desktop/TempDir/" .

+5
source

All Articles