I am running a script in my project and I see this command

su - `whoami`

I am having a few basic questions:

  1. What does this command do ?
  2. What is the significance of `` quotes outside the whoami command ?
  3. What is - used for ?

I know su stands for switch user.

  • 1
    1. This command does a full login. Probably to reset the environment. 2. The quotes execute the command and insert the returned value in that position. So if your user name is xyz, the command gets su - xyz 3. The - is the same as -l, see man su. Basically it simulates a full login. – groxxda Jul 25 '14 at 9:36
  • See Michael Homer's answer for a more thorough explanation. ;-) – groxxda Jul 25 '14 at 9:40
  • @Groxxda I am just going through that. – sachinjain024 Jul 25 '14 at 9:40

The backticks (``) are command substitution: they are replaced by the result of running the command inside the backticks. Here they run whoami, which prints your username.

The - after su makes su run a login shell: a login shell will read certain environment configuration from scratch, among other things. By default it would just run the user's shell as an ordinary shell, which won't do all that work (see the link).

su always starts a new user session, reauthenticating the user as though they'd logged in from scratch. Any environment variables from the outside are cleared out in the inner shell.

So all together this equates to the command (supposing your username on the machine is also "blunderboy"):

su - blunderboy

which means "reauthenticate a new session as blunderboy and run a login shell".

The overall result of this command is to start a new session as though from scratch, as the current user. Why bother to do that? Likely to pick up on some side effects of su: in particular, because it starts a new user session, it will pick up on any changes to the groups the user is in, as well as other user or permission changes, and and environment variables are all reset. Ordinarily, adding a user to a new group won't take effect until they log in again¹: su counts as logging in again for that purpose, so the shell it starts will have the new group active.

¹ This is a simplification, but true enough for this explanation.

  • Thanks Mike for such a explanation. Just one more doubt what is the difference between `` and $ then ? because I used $ in my script for evaluating a command and getting its output in a variable – sachinjain024 Jul 25 '14 at 9:42
  • 1
    ``` some command ``` is essentially the same as $(some command), and they both do command substitution (see link in post). $varname is how you access a shell or environment variable. – Michael Homer Jul 25 '14 at 9:44
  • Yes, that is what I intend to do..Relogin to session after declaring some exports So I understand the case now..Thanks – sachinjain024 Jul 25 '14 at 9:47

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.