Can you su in a shell script?

No you can’t. Or atleast… you can su but su will simply open a new shell at that point and when it’s done it will continue with the rest of the script.

What is su in bash?

The Unix command su, which stands for ‘substitute user’ (originally ‘superuser’ ), is used by a computer user to execute commands with the privileges of another user account. When executed it invokes a shell without changing the current working directory or the user environment.

What is su root?

The su (short for substitute or switch user) utility allows you to run commands with another user’s privileges, by default the root user. Using su is the simplest way to switch to the administrative account in the current login session. In this tutorial, we will explain how to use the su command.

How do you use the su command?

The su command lets you switch the current user to any other user. If you need to run a command as a different (non-root) user, use the –l [username] option to specify the user account. Additionally, su can also be used to change to a different shell interpreter on the fly.

How do you su to another user in a script?

Using the command su [username] allows you to run a command in the context of that user, from the current location. Using the following command su – [username] switches you to that users home directory. If you omit the username you are switching to root.

How use sudo su command in Linux?

The su command switches to the super user — or root user — when you execute it with no additional options. You’ll have to enter the root account’s password. This isn’t all the su command does, though — you can use it to switch to any user account.

What does su do in terminal?

su , on the other hand, is an acronym for switch user or substitute user. You are basically switching to a particular user and you need the password for the user you are switching to. Most often, the user account you switch to is the root account but it can be any account on the system.

Is su the same as root?

The su command switches to the super user — or root user — when you execute it with no additional options. Su switches you to the root user account and requires the root account’s password. Sudo runs a single command with root privileges — it doesn’t switch to the root user or require a separate root user password.

What is the difference between sudo and su?

This is a key difference between su and sudo. Su switches you to the root user account and requires the root account’s password. Sudo runs a single command with root privileges — it doesn’t switch to the root user or require a separate root user password.

Why does the SU Command not make a script root?

The commands in a script execute one by one, independently. The Script itself as the parent of all commands in the script, is another independent process and the su command does not and can not change it to root: the su command creates a new process with root privileges.

How do I use multiple su commands in a script?

You can use a here document to embed multiple su commands in your script: Not like this. su will invoke a process, which defaults to a shell. On the command line, this shell will be interactive, so you can enter commands. In the context of a script, the shell will end right away (because it has nothing to do).

Is Su a command in Linux interactive?

Not like this. su will invoke a process, which defaults to a shell. On the command line, this shell will be interactive, so you can enter commands. In the context of a script, the shell will end right away (because it has nothing to do).

What happens to the parent process after su command completes?

After that su command completes, the parent process, still running as the same user, will execute the rest of the script. What you want to do is write a wrapper script. The privileged commands goes into the main script, for example ~/main.sh

You Might Also Like