1

Is there a way to run something like su -l that reads all the login shell files but doesn't change the directory that you're in?

For some motivation, I'm trying to create a Slackware Linux package. Most of the time packages will build correctly if you just use su. However, there are some packages like sbcl that depend on TeX tools and will fail without su -l. I prefer using su when I can get away with it because it doesn't change the current directory.

If I'm in a directory like ~/opt/haskell-stack-slackbuild.

$ pwd
/home/<user>/opt/haskell-stack-slackbuild

If I just run su I'm back in the same directory, but if I run su -l then I get dropped in /root regardless of where I was and have to navigate back to the directory I was working in.

$ su -l
# pwd
/root

1 Answer 1

2

Why not just source root's .profile after you are running in the root shell?

$ su
Password: 
# . ~root/.profile
# 

Of course you can source any script from anywhere once in the root shell, and thus fix or change the root shell environment in any way that is necessary.

There are many ways one could inject different settings into the root shell directly from the origin shell using the fact that su accepts "shell parameters" on its command line. Here's an example that gives the su command the instructions to run the root shell in such a manner as to source root's profile and then start a shell with the environment from that profile (which, because of the exec, replaces the shell started by su):

$ su -- root -c ". /root/.profile; exec /bin/sh" 
Password: 
# 
3
  • oh interesting. Is there a way to do that as one command so I can hide it behind an alias? Commented Mar 2, 2017 at 19:41
  • I think I found a better one. su -- root -c "exec /bin/bash -l". (I think my default shell is specifically not bash not sh which happens to point to bash) Keeps me in the same directory, but sources /etc/profile or /root/.profile or others as appropriate. Commented Mar 2, 2017 at 20:02
  • as I said, there are many ways! :-) Commented Mar 2, 2017 at 20:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .