Q:

Change user AND environment in bash script

Why does this work (as root):
$su - aba
~> echo $JAVA_HOME

Ausgabe: /usr/java/jdk1.7.0_45
And not this shell script (also run as root):
#!/bin/bash
su - aba << EOF
echo $JAVA_HOME > tmp
EOF

Output:
$more tmp

$

Question:
How can I make this work? Or better: My goal is to execute some commands as another user and also use this enviroment of this user and maybe even change his enviroment (only temporary).
EDIT
In the meantime I succeeded with runuser - aba -c 'echo JAVA_HOME', altough it's not in all aspects what wished for, because I still can only transfer one command-line.

A:

su - aba
is short for 
su --login ada
which doesn't make sense inside a script. Since you don't have a terminal once it is run it goes back to the previous user (root).
try
su -c "echo $JAVA_HOME" ada
but even this won't run the users .profile or .bashrc.
However you are root and you should be able to parse those files for the proper settings.
eg.
TEMP_JAVA=$(grep JAVA_HOME /home/$USER/.bashrc)
JAVA_HOME=${TEMP_JAVA##*=}
echo $JAVA_HOME

