Command not found in Bash's IF-ELSE condition when using [! -d “$DIR”]

前端 未结 3 1163
感动是毒
感动是毒 2020-12-20 15:39

I have a code like this

#!/bin/bash 
DIR=\"test_dir/\";
if [! -d \"$DIR\"]; then
    # If it doesn\'t create it
    mkdir $DIR
fi

But why

3条回答
  •  猫巷女王i
    2020-12-20 15:56

    Add space between [ and !. And before ] as well.

    #!/bin/bash 
    DIR="test_dir/";
    if [ ! -d "$DIR" ]; then
        # If it doesn't create it
        mkdir $DIR
    fi
    

    It's also a good idea to quote your variable:

        mkdir "$DIR"
    

提交回复
热议问题