Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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):

su - aba << EOF
echo $JAVA_HOME > tmp


$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.

share|improve this question
up vote 1 down vote accepted

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).


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.


TEMP_JAVA=$(grep JAVA_HOME /home/$USER/.bashrc)

share|improve this answer
Ok, grep is a good idea. I can work with that. In the meantime I also succeeded with runuser - aba -c command. – Nils-o-mat Apr 14 '14 at 14:27
I would vote you up, but I'm a newbie here and and not allowed to ;-) – Nils-o-mat Apr 14 '14 at 14:32
Find some Java stuff and answer. I was doing the same with bash stuff. Already made upvote stage, have reached comments on Unix&Linux. – Michael Tomkins Apr 17 '14 at 0:18

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.