su v/s su - command

The su and su - commands in Linux are both used to switch to another user, typically the root user, but they behave differently in terms of the environment they provide.

su command

The su command (short for "switch user") switches the current session to another user account. When used without specifying a username, it defaults to the root account.


Basic Usage:


$ su


This prompts for the root password and opens a new shell as the root user without loading the root user's environment settings, inheriting the environment variables (e.g., PATH) from the original users.


Switching to Another User:


$ su username


This will prompt for the user's password, then open a new shell for user without loading her login environment.

su - command

The su - command provides a "login shell" for the target user, meaning it simulates a full login and loads the target user's environment.


Basic Usage:


$ su -


This prompts for the root password, then switches to a root login shell with root's environmental variables, PATH, HOME, etc., as if the root user had logged in directly.


Switching to Another User with su -


$ su - username


This switches to the user account with his/her full login environment and loads users shell settings, such as environment variables, working directory, and login scripts like .bash_profile and more


Summary of Differences


Command

Environment

Working Directory

Example

su

Inherits the calling user's environment

Stays in the current directory

su
su username

su - 

Loads the target user's full environment (Login Shell)

Switches to the target user's home directory

su -

su - username