I have some question. I'm trying to create some bash script to automatically some task. I need to do the first step of the task as normal user, and su - to be root to finish the task.

I've been wondering if there is a way to do that? Until now i'm only been able to start the bash as normal user, complete the first step, ask for su - password, but after it connect as root, the script stop.

Is it possible to proceed with the bash after been logged as root?

Thank you in advance.

ps : If I'm not precise, or if you need some information, feel free to ask, I'll do my possible to correct those mistake.


Have you tried using sudo somecommand instead of trying to switch users? Your user will need to be in the 'wheel' user group (depending on your system) to do that.

su changes the environment (user variables etc.) so, though I haven't tested this, you probably can't do it in a shell script.

Your shell script was

#Do these commands as a normal user
echo "Hello World"
cat ./somefile

#Do these commands as root
su -
echo "Hello again"
cat /etc/somesystemfile

try changing it to

#Do these commands as a normal user
echo "Hello World"
cat ./somefile

#Do these commands as root
sudo echo "Hello again"
sudo cat /etc/somesystemfile
  • No I didn't, I often forget about the sudo. Thanks I'll try this. Jun 20 '11 at 11:48
  • some command don't work with sudo (well, at least I get error) like cd... Jun 20 '11 at 12:00
  • @Anarko You shouldn't have to run commands like CD with sudo as they don't require root permissions, so you can CD to a directory as your normal user then use sudo to run commands. I think.
    – Smudge
    Jun 20 '11 at 12:02
  • 2
    Sudo is meant for executing a single command. Using consecutive sudo commands and changing path in one of them, with cd, is not going to work. I agree with other posters: execute the whole script with sudo and forget about sudo'ing the individual commands inside your script.
    – wzzrd
    Jun 20 '11 at 12:05
  • 1
    @wzzrd I agree, or if some commands have to be run as the normal user (not sure why) split the shell script into two parts, one to run as normal user and one to run as root.
    – Smudge
    Jun 20 '11 at 12:08

Start the process as root, then execute normal user tasks with su (-) user -c ..command.., and finish it as root. This way it won't even ask for password.


If there is a specific command you wish the script to be able to execute as root, while it's running as another_user then you should use sudo.

Configure sudo to allow another_user to execute the specific command, something like this in your sudoers file,

someuser ALL=/path/to/specific_command

and then in your bash script use,

sudo /path/to/specific_command -some -args

If you want your bash script to run lots of commands as root, or run general commands such as mv or cp you might want to come at this from a different angle. Write the script so that it runs as root (in root's crontab for example), and have it execute the non-root commands using 'su - someuser' because root can do that without putting passwords in.


I find the answer, just have to use the su - -c "" cmd.

Exemple :

tar /home/me /dev/null

su - -c "yum update -y"

tar /home/me /dev/null

su - -c "
yum install xterm -y
yum clean-all
yum remove xterm -y


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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