26

When I am working on our RHEL machines, I use sudo su - to switch to being root. One day, a typo meant I typed sudo su -- instead - it seems to me that everything was the same as with a single hyphen, except that I was in the same folder as before I issued the command (with a single hyphen I find myself in /root).

Are there other differences? Is it safe to use this in scenarios where I know I want to work in the same directory?

1 Answer 1

28

When you provide a double-hyphen the experience you will have is identical to if you had just executed sudo su without any hyphen.

Passing a single hyphen is identical to passing -l or --login. The man page for su describes the behavior as:

Provide an environment similar to what the user would expect had the user logged in directly.

This includes setting your directory to your home directory and setting a bunch of other environment variables.

Passing a double-hyphen to a command is typically used to mark the end of command-line flags and the beginning of non-flag arguments. For example, if you run touch -R you'll receive an error saying that -R is not an option to touch, but if you run touch -- -R it will create a file named -R. This is true of many command-line tools (ls -R will do a recursive ls whereas ls -- -R will perform an ls on a file or directory named -R.

So, to wrap this up, when you pass only -- to su it is basically ignoring the -- and acting like you did not pass any option at all.

4
  • I will create a question from it but sudo su - and sudo su -l differs. On an account where I ssh into with a key only, and I have sudo rights to change to a specific other user, sudo su - changes the user without problems but sudo su -l or sudo su --login will ask for a sudo password, which I don't have (although by examining sudo -l I could execute /bin/su with NOPASSWD) Commented Jun 14, 2016 at 12:49
  • @karatedog, it looks like you didn't create a question.... Commented Oct 25, 2016 at 2:12
  • @karatedog I'm a bit late to the party here, but I'd really like to see that question and any answers that come in. I'm wondering about this myself due to some ambiguous and/or confusing text in the su man page. Commented Oct 18, 2021 at 19:12
  • 1
    Oh, this was 5 years ago! If you have more up-to-date, reproducible example with a fresh operating system, please go ahead and create that question. Commented Oct 19, 2021 at 15:06

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.