Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I'm quite new to Linux terminal and I'm not quite sure what the difference between su with a hyphen and su without a hyphen is, for example: su - username vs. su username.

I looked into the documentation but in there, this was not mentioned. Could someone please help me out?

share|improve this question
    
a "login shell" often means interactive shell. This is vs. a shell that just executes a script and exits. –  Visa is Racism Jul 26 '12 at 16:41
4  
It's right there in the same manpage you link to: "-, -l, --login: make the shell a login shell" –  ArjunShankar Jul 26 '12 at 16:43
    
you don't even have to google it, just type su --help. –  Art Shayderov Jul 26 '12 at 16:51
    
Of noteworthyness: This is particularly useful when su-ing to root as without using the hypen to start a new login shell, your $PATH won't get updated and thus you won't be able to directly call root-only binaries in /sbin and /usr/sbin –  Garrett Jul 26 '12 at 16:55
add comment

migrated from stackoverflow.com Jul 26 '12 at 16:45

This question came from our site for professional and enthusiast programmers.

2 Answers

The difference between "-" and "no hyphen" is that the latter keeps your existing environment (variables, etc); the former creates a new environment (with the settings of the actual user, not your own).

https://wiki.archlinux.org/index.php/Su

The hyphen has two effects:

1) switches from the current directory to the home directory of the new user (e.g., to /root in the case of the root user) by logging in as that user

2) changes the environmental variables to those of the new user as dictated by their ~/.bashrc. That is, if the first argument to su is a hyphen, the current directory and environment will be changed to what would be expected if the new user had actually logged on to a new session (rather than just taking over an existing session).

share|improve this answer
    
this site -> admon.org/difference-between-login-shell-and-non-login-shell says that when used without the hyphen the files /etc/profile and ~/.bash_profile are not executed. What significance do these two files have on the user environment ? –  user574183 Jul 26 '12 at 17:02
    
Those two things are the files that set the user environment :) Not executing them means keeping your original environment. Executing them (by using "-") means making your environment the same as the user you're "su'ing" to. Using the hyphen is usually what you want - I use "-" habitually whenever I "su". –  paulsm4 Jul 26 '12 at 18:40
    
does the order of execution really matter ? Iam asking this because on that site they have mentioned that the two files /etc/bashrc ~/.bashrc are executed in reverse order when in a non login shell. Somebody please help me –  user574183 Jul 28 '12 at 18:03
add comment

su - starts a login shell for root. For the differences to a normal shell, see this site for example.

share|improve this answer
    
That's true, but it's not the question :) –  paulsm4 Jul 26 '12 at 18:41
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.