title: Set JAVA_HOME from bash script
body: <p>I'm trying to write a script that switches between the JDK and the JRE and also x86 and x64 architectures.</p>
  
  <p>However, when I do: <code>echo $PATH</code> or <code>echo $JAVA_HOME</code> or <code>echo $JRE_HOME</code>, nothing prints! Path prints but does not have java added to it.</p>
  
  <p>My script is as follows and it exports the paths at the very end.</p>
  
  <pre><code>#!/bin/bash
  
  #sudo apt-get install oracle-java8-installer
  
  x64_JDK_PATH="/opt/java/jdk1.7.0_51"
  x64_JRE_PATH="$x64_JDK_PATH/jre"
  
  x86_JDK_PATH="/opt/java/jdk1.7.0_51_x86"
  x86_JRE_PATH="$x86_JDK_PATH/jre"
  xJAVA_PATH=""
  
  echo
  echo -e "What would you like to switch to?"
  echo -e "    1. JDK (Java Development Kit)\n"
  echo -e "    2. JRE (Java Runtime Environment)\n"
  echo -e "Choice: \c"
  read ENV_ANSWER
  echo
  
  case $ENV_ANSWER in
      1)
          echo -e "Switching to the JDK (Java Development Kit)\n"
      ;;
  
      2)
          echo -e "Switching to the JRE (Java Runtime Environment)\n"
      ;;
      *)
          echo -e "Invalid option.\n"
          echo -e "Script terminating.\n"
          exit
      ;;
  esac
  
  echo -e "What version would you like to switch to? (x32\\\x64): \c"
  read ARCH_ANSWER
  
  if [ $ARCH_ANSWER != "x32" ] &amp;&amp; [ $ARCH_ANSWER != "x64" ]
  then
      echo -e "Invalid version. Versions are 'x32' and 'x64'."
      echo -e "Script terminating.\n"
      exit
  fi
  
  echo "Switching to the $ARCH_ANSWER version."
  echo
  
  if [ $ARCH_ANSWER = "x32" ] &amp;&amp; [ $ENV_ANSWER = 1 ]
  then
      xJAVA_PATH=$x86_JDK_PATH
  elif [ $ARCH_ANSWER = "x32" ]
  then
      xJAVA_PATH=$x86_JRE_PATH
  fi
  
  if [ $ARCH_ANSWER = "x64" ] &amp;&amp; [ $ENV_ANSWER = 1 ]
  then
      xJAVA_PATH=$x64_JDK_PATH
  elif [ $ARCH_ANSWER = "x64" ]
  then
      xJAVA_PATH=$x64_JRE_PATH
  fi
  
  sudo update-alternatives --install "/usr/bin/java" "java" "$xJAVA_PATH/bin/java" 1
  sudo update-alternatives --install "/usr/bin/javaws" "javaws" "$xJAVA_PATH/bin/javaws" 1
  if [ $ENV_ANSWER = 1 ]
  then
      sudo update-alternatives --install "/usr/bin/javac" "javac" "$xJAVA_PATH/bin/javac" 1
      sudo update-alternatives --install "/usr/bin/javadoc" "javadoc" "$xJAVA_PATH/bin/javadoc" 1
  fi
  sudo update-alternatives --set java "$xJAVA_PATH/bin/java"
  
  
  echo
  echo -e "Would you like to update your Mozilla Plugins as well? (y\\\n): \c"
  read PLUGINS_ANSWER
  
  if [ $PLUGINS_ANSWER = "Y" ] || [ $PLUGINS_ANSWER = "y" ]
  then
      rm "$HOME/.mozilla/plugins/libnpjp2.so"
      mkdir -p "$HOME/.mozilla/plugins"
      ln -s "$xJAVA_PATH/jre/lib/amd64/libnpjp2.so" "$HOME/.mozilla/plugins/"
      echo -e "Mozilla Plugins updated.\n"
  else
      echo -e "Mozilla Plugins left untouched.\n"
  fi
  
  JAVA_HOME=$xJAVA_PATH
  JRE_HOME=$xJAVA_PATH
  PATH=$PATH:$HOME/bin:$JAVA_HOME/bin
  export JAVA_HOME
  export JRE_HOME
  export PATH
  
  java -version
  echo
  </code></pre>
  
  <p>Any ideas what is wrong with it? <code>java -version</code> prints fine. Everything works except for the exporting of the paths..</p>