Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

[-d: command not found [closed]

Tags:

bash

shell

As per this answer: Unix Bash Shell Programming if directory exists, I'm trying to check if a directory exists. However, when I run this, I get line 1: [-d: command not found. What am I doing wrong here?

if [-d "~/.ssl"]; then
  echo '~/.ssl directory already exists'
else
  sudo mkdir ~/.ssl/
fi
like image 365
user886596 Avatar asked Mar 31 '14 17:03

user886596


1 Answers

[-d

is not a command.

[ -d

is the test command with the -d option.

Space matters.

(Also, the [ command needs to end with a ] parameter, which likewise has to be separated from other arguments by whitespace.)

That's the crux of the matter. There is another issue, though: If you quote the tilde, it doesn't expand. (This is one of the rare place where you may want to avoid quotes.) Quotes are great, though, so why not write "$HOME/.ssl"? (There's a subtle difference between ~ and "$HOME", but it doesn't matter for most uses.)

Honestly, all you really need is probably:

if mkdir -p ~/.ssl; then
  # Do stuff with new directory
else
  # Handle failure (but keep in mind `mkdir` will have its own error output)
fi
like image 188
kojiro Avatar answered Sep 28 '22 18:09

kojiro