That's because the
HADOOP_HOME variable isn't exported:
$ cat foo.sh
echo "HADOOP_HOME: $HADOOP_HOME"
$ echo $HADOOP_HOME
$ export HADOOP_HOME
When you run a shell script, that script will run in its own bash instance (that's what the
#!/bin/bash does) that is a child shell of the current one. Variables are not passed to child shells by default, only if they are exported. Think of each bash session as independent (they largely are). You usually don't want variables defined in one to pollute the environment of another. For those cases where that is necessary, use