diff --git a/share/spack/setup-env.sh b/share/spack/setup-env.sh
index e3dd1cb4b9a2e90aba2e0c4afd587450b4255ef2..11a4c0a70c4d79a7586f978cd8844856be86bba5 100755
--- a/share/spack/setup-env.sh
+++ b/share/spack/setup-env.sh
@@ -84,10 +84,10 @@ function spack {
             if [ "$_sp_arg" = "-h" ]; then
                 command spack cd -h
             else
-		LOC="$(spack location $_sp_arg "$@")"
-		if [[ "x" -ne "x$LOC" ]] ; then
+                LOC="$(spack location $_sp_arg "$@")"
+                if [[ -d "$LOC" ]] ; then
                     cd "$LOC"
-		fi
+                fi
             fi
             return
             ;;