How can I check if a directory exists in a Bash shell script?


One has to always remember that always wrap variables in double-quotes when referencing them in a Bash shell script. So always run your script with $DIRECTORY which you have to set to "My M0viez" and after that, your script will blow up. So You don’t want that. And to avoid this one should use this.

if [ -d "$DIRECTORY" ]; then
    # Will enter here if $DIRECTORY exists, even if it contains spaces


Also, read How do I rename a local Git branch?

Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *