0

I want to perform a test: if a certain variable exists, is non-empty, and its value matches an existing directory, then "exists" should be outputted.

I did the following:

$ echo $var

$ if [ -d $var ]; then echo "exists"; fi
exists

I got "exist", although I did not assign any value to $var. Why did this happen, and how do I test this properly?

screenshot

  • If @WillW answered your question, please mark it as accepted! :) – Swivel Jun 4 at 14:42
3

I think you need to wrap $var in quotes.

if [ -d "$var" ]; then echo "exists"; fi

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.