I was experimenting with switching users with the su command. I'm confused why su carrying the SHELL environment variable over to a user monty. The exercise is shown below:

vagrant@cats:/home$ cat /etc/passwd | grep -E "monty|vagrant"
vagrant@cats:/home$ cat /etc/default/useradd | grep SHELL
# The SHELL variable specifies the default login shell on your
vagrant@cats:/home$ echo $SHELL
vagrant@cats:/home$ ssh monty@localhost
monty@localhost's password:
Welcome to Ubuntu 16.04.1 LTS (GNU/Linux 4.4.0-31-generic x86_64)

* Documentation:  https://help.ubuntu.com
* Management:     https://landscape.canonical.com
* Support:        https://ubuntu.com/advantage
Last login: Wed Sep 28 17:34:36 2016 from ::1
$ echo $SHELL
$ exit
Connection to localhost closed.
vagrant@cats:/home$ su monty
monty@cats:/home$ echo $SHELL
monty@cats:/home$ exit

As can be seen above:

  • There are at least two users vagrant and monty
  • vagrant has a specified shell of /bin/bash
  • monty has an unspecified shell which should default to /bin/sh as shown in /etc/default/useradd file
    • this is backed up by sshing into monty and checking the shell
  • except then weirdly why using su monty the SHELL env variable stays as /bin/bash

That last bullet is the main reason why I'm confused. Taking a look at man su doesn't seem to help either, notice the -s flag:

-s, --shell SHELL
           The shell that will be invoked.

           The invoked shell is chosen from (highest priority first):

               The shell specified with --shell.

               If --preserve-environment is used, the shell specified by the $SHELL environment variable.

               The shell indicated in the /etc/passwd entry for the target user.

               /bin/sh if a shell could not be found by any above method.

Going down the list:

  • I didn't use the -s or --shell flag, so that doesn't explain anything
  • I also didn't use the --preserve-environment switch which would explain the situation, but doesn't because I didn't use it
  • There is no shell in the /etc/passwd for the target user, which should mean it defaults to /bin/sh, so that doesn't help
  • It would be make sense for a lack of an entry and all the above fags or switches to cause nothing to be found, except then /bin/sh would be used...which it isn't, so that doesn't help either

So I'm really confused, it seems like I'm literally seeing a perfect inversion of any behavior one could expect after reading through every config file and man entry on this issue haha.

Would love some help and guidance :)

Edit: See answer and comment below. Also see the first and second to last paragraph in the DESCRIPTION section in the man page


...The optional argument - may be used to provide an environment similar to what the user would expect had the user logged in directly.


The current environment is passed to the new shell...


1 Answer 1


su by default preserves the current user's environment when switching to another user, this includes the current user's shell.

To use the target user's environment and configuration, you need to start a login shell by adding the -l/--login option or simply a -:

su - monty

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .