14

From su's man page:

For  backward  compatibility, su defaults to not change the current directory
and to only set the environment variables HOME and SHELL (plus USER and LOGNAME
if the target user is not root).  It is recommended to always use the 
--login option (instead of  its  shortcut -) to avoid side effects caused
by mixing environments.

...

-, -l, --login
    Start the shell as a login shell with an environment similar to a real login:

        o      clears all the environment variables except TERM

        o      initializes the environment variables HOME, SHELL, USER, LOGNAME, and PATH

        o      changes to the target user's home directory

        o      sets argv[0] of the shell to '-' in order to make the shell a login shell

It's hard to tell if there's any difference between - and --login (or supposedly just -l). Namely, the man page says "instead of its shortcut -", but all these options are grouped together, and I don't see an explanation of the difference, if it exists at all.

UPD I checked the question, which is supposed to solve my problem. The question is basically about difference between su and su -. And I'm asking about difference between su - and su --login. So no, it doesn't solve it at all.

  • 3
    I think this sentence is poorly written. It should be To avoid side effects caused by mixing environments use --login option or its equivalent -. The first form is recommended because - can be placed only just before username. – jimmij Oct 24 '16 at 16:50
  • I don't know your definition of duplicate, but I don't consider my question as such. The question you suggest is basically about difference between su and su -. And I'm asking about difference between su - and su --login. What am I missing here? – x-yuri Oct 25 '16 at 5:31
11

Debian's manual entry seems to be more enlightening:

   -, -l, --login
       Provide an environment similar to what the user would expect had the user logged
       in directly.

       When - is used, it must be specified before any username. For portability it is
       recommended to use it as last option, before any username. The other forms (-l
       and --login) do not have this restriction.
  • I'd say they complement each other. The one from upstream suggests to avoid using -. Speaking of which, why "before any username"? – x-yuri Oct 24 '16 at 17:10
  • 7
    "before any username" can be rephrased as "before the username, if any" – Mark Plotnick Oct 24 '16 at 17:48
  • @MarkPlotnick You seem to be wrong. – x-yuri Oct 26 '16 at 2:04
  • @x-yuri that whole discussion, comes to the conclusion that Mark Plotnick is correct, and the manual is, at best, logically vague. (though most of us did not notice) – ctrl-alt-delor Oct 28 '16 at 15:46
  • I disagree, however, with the general point that, "before any username" can be rephrased as "before the username, if any." Perhaps Mr. Plotnik meant that the man page* should have rephrased the text using his suggestion, but the two phrases are not equivalent (as I explained at the beginning of this post). Meaning, Mark Plotnick knows how su works, but "before any username" != "before the username, if any." That's the conclusion the discussion came to, as I see it. – x-yuri Oct 28 '16 at 18:09
0

there is no difference, it's like running;

echo bob | grep -v bob
echo bob | grep --invert-match bob

just shorthand...

  • 3
    I think he is asking for clarification of the to avoid side effects caused by mixing environments. part of the man page. What side effects? – Zachary Brady Oct 24 '16 at 16:38
  • Not really. Side effects are more or less clear, and they apply when not using any of -, -l, --login. My question is about why it's not recommended to use -, as opposed to '-l, and --login`. – x-yuri Oct 24 '16 at 16:52
  • the portability issue would be if you are writing script, i wouldn't reccomend using su at all in a script, but there could be a use case, regardless the most portable option in a script would be -l or --login – mikejonesey Oct 24 '16 at 17:31
  • Why not use su at all, when writing a script? – x-yuri Oct 25 '16 at 4:48
  • why not write a script as the user that owns the data? It's most rare there is a usecase where you need to perform actions as another user, and then there is sudo (which has limitations on commands and params used). – mikejonesey Oct 25 '16 at 8:23

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.