Assume we have two users: user1 (an admin) and user2 (a standard user)
- Login as user1
Run
sudo su - user2 -c "env"The result shows $HOME=/home/user2
Run
sudo su - user2 -c "echo $HOME"The result shows $HOME=/home/user1
Why is that?
Assume we have two users: user1 (an admin) and user2 (a standard user)
Run
sudo su - user2 -c "env"
The result shows $HOME=/home/user2
Run
sudo su - user2 -c "echo $HOME"
The result shows $HOME=/home/user1
Why is that?
This is because in the second example, the variable $HOME gets expanded before the shell executes the command, so what you are really running is
sudo su - user2 -c "echo /home/user1"
as that is the value the variable $HOME has at this point.
(I also guess the output of the second command is not really $HOME=/home/user1 but instead just /home/user1).
You could prevent variable expansion in the first shell if you use single quotes:
$ sudo su - user2 -c 'echo $HOME'
/home/user2