Howto switch / chage user id witin a bash script to execute commands in the same script?

a1an

Is there a way to switch user identity within a script (executed as root as part of an installation process) to execute some commands without calling an external script, then return to root to run other commands?

Sort of:

#!/bin/bash
some commands as root
SWITCH_USER_TO user
some commands as user including environment variables checks, without calling an external script
SWITCH_USER_BACK
some other stuff as root, maybe another user id change...
Ignacio Vazquez-Abrams

No. But you can use sudo to run a shell and use a heredoc to feed it commands.

#!/bin/bash
whoami
sudo -u someuser bash << EOF
echo "In"
whoami
EOF
echo "Out"
whoami

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Bash script to switch the user

From Dev

Execute gcloud commands in a bash script

From Dev

Bash script to spawn and execute commands

From Dev

Cannot execute shell commands in bash script

From Dev

How to execute git commands from bash script?

From Dev

bash script execute commands after ssh

From Dev

Bash script to read from a file and execute commands

From Dev

Bash script to read from a file and execute commands

From Dev

How to execute the following commands in a bash script?

From Dev

Bash script to execute multiple Unix commands

From Dev

Howto debug running bash script

From Dev

howto run a bash compressed script?

From Dev

How to execute sudo commands with Expect & send commands in bash script?

From Dev

Bash Script execute commands from a file but if cancel on to jump on next one

From Dev

Bash script: How to execute commands consecutively without waiting for the previous one?

From Dev

Shell out into zsh and execute commands from bash script

From Dev

How to execute commands in docker container as part of bash shell script

From Dev

Bash script: How to execute commands consecutively without waiting for the previous one?

From Dev

How to execute shell commands in a loop from within a bash script?

From Dev

Bash script does not execute commands until after delay

From Dev

execute command as root and then switch back to user in shell script

From Dev

bash howto script - if no answer or mutliline answer

From Dev

Optional commands for bash script

From Dev

If I sudo execute a Bash script file, will all commands inside the Bash script be executed as sudo as well?

From Dev

If I sudo execute a Bash script file, will all commands inside the Bash script be executed as sudo as well?

From Dev

Execute script as a specific user

From Dev

Go: execute bash script

From Dev

How to execute a bash script?

From Dev

Bash Script - Will not completely execute

Related Related

  1. 1

    Bash script to switch the user

  2. 2

    Execute gcloud commands in a bash script

  3. 3

    Bash script to spawn and execute commands

  4. 4

    Cannot execute shell commands in bash script

  5. 5

    How to execute git commands from bash script?

  6. 6

    bash script execute commands after ssh

  7. 7

    Bash script to read from a file and execute commands

  8. 8

    Bash script to read from a file and execute commands

  9. 9

    How to execute the following commands in a bash script?

  10. 10

    Bash script to execute multiple Unix commands

  11. 11

    Howto debug running bash script

  12. 12

    howto run a bash compressed script?

  13. 13

    How to execute sudo commands with Expect & send commands in bash script?

  14. 14

    Bash Script execute commands from a file but if cancel on to jump on next one

  15. 15

    Bash script: How to execute commands consecutively without waiting for the previous one?

  16. 16

    Shell out into zsh and execute commands from bash script

  17. 17

    How to execute commands in docker container as part of bash shell script

  18. 18

    Bash script: How to execute commands consecutively without waiting for the previous one?

  19. 19

    How to execute shell commands in a loop from within a bash script?

  20. 20

    Bash script does not execute commands until after delay

  21. 21

    execute command as root and then switch back to user in shell script

  22. 22

    bash howto script - if no answer or mutliline answer

  23. 23

    Optional commands for bash script

  24. 24

    If I sudo execute a Bash script file, will all commands inside the Bash script be executed as sudo as well?

  25. 25

    If I sudo execute a Bash script file, will all commands inside the Bash script be executed as sudo as well?

  26. 26

    Execute script as a specific user

  27. 27

    Go: execute bash script

  28. 28

    How to execute a bash script?

  29. 29

    Bash Script - Will not completely execute

HotTag

Archive