Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

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

I'm running a remote bash script (using ssh -t 'bash'). In "" I have a sudo su anotheruser. At that point, the script seems to push a shell and I'm left in that "interactive" script and the rest of my script ( doesn't run unless I type "exit". Is there a workaround that gets around this problem?

Thanks in advance.

Here is the contents of my test script "":

sudo su diy
share|improve this question
possible cross site duplicate of:… – Ciro Santilli 巴拿馬文件 六四事件 法轮功 May 24 '14 at 6:31
up vote 3 down vote accepted

sudo starts a shell unless you instruct it otherwise.

It looks like you actually want to run this script as another user. To do that, try something like this:

if [ `id -nu` != diy ]; then
    sudo -u diy $0 # Re-run this script as user diy
    # Everything you want to do goes here

Keep in mind that /etc/sudoers must be set up to allow the original user to run this script as the new user.

share|improve this answer
Allthough for bash (not bourne shell) it's more convenient to use if [[ $(id -un) != diy ]]; then.. form of checking. – val0x00ff Jan 14 '13 at 13:08

su as used in you script only runs the su line itself as the other user.

Use su - otheruser -c "cmds to execute".

share|improve this answer

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.