Since the empty string cannot name an existing directory, you don't need to worry if MY_DIR
is set or not; just check if its (possibly nonexistent) value names a directory.
if [[ ! -d $MY_DIR ]]; then
export MY_DIR=$PWD
fi
There's also no need to check specifically if MY_DIR
is in the environment. While it is possible to have a shell variable that is not an environment variable, it is not possible to have an environment variable that is not also a shell variable[0], nor is it possible to have a shell variable with the same name as an environment variable but a different value (an environment variable is simply a shell variable with its export attribute set.)
[0] OK, technically you could inherit an environment variable whose name is not a valid shell identifier. Let's just ignore those, shall we? :)