Shell and su command

2

1

Can someone please explain why when i issue:

su
echo $0

it says:

bash

when

su -
echo $0

it says:

-su

? Am i right that in the 1-st case (just su) current shell spawns a new non-interactive shell while in the second case (su -) it just stays with the current shell?

Mulligan

Posted 2017-01-16T10:34:58.017

Reputation: 533

Answers

2

When you using just su you inheriting environment of user that running su. In contrast, when you using su - (it is shortcut for -login option of su) it switching you "completely" to root's account and setting up environment in the same way as if it happen if you login directly to root

Alex

Posted 2017-01-16T10:34:58.017

Reputation: 5 606

1

Alex's answer is correct, but he hasn't really explained what you're seeing. When you run su - it's importing the environment of the user you are switching to (root in this case). The last command you ran as root must have been su.

Darren

Posted 2017-01-16T10:34:58.017

Reputation: 2 435