How to login as another user and then log out in bash script?

10,357

The simplest way is to make the stuff that has to run as the other user a separate script and invoke it with the "-c" option in su, like su -c otherscript userid.

You may be able to do that as a "here script" with << EOF, but I've never tried it.

Share:
10,357
Neuquino
Author by

Neuquino

SOreadytohelp just ask me what you want to know

Updated on June 09, 2022

Comments

  • Neuquino
    Neuquino almost 2 years

    I need to write a bash script to do something as another user and then return to the initial user...

    Suppose I run the following as root:

    #!/bin/bash
    USER=zaraza
    su - "${USER}"
    #do some stuff as zaraza
    ________ #here I should logout zaraza
    #continue doing things as root
    

    In the console I should write "exit", but in bash is a keyword and it exits the script...

    Thanks in advance,