Sign up ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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

2 Answers 2

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.