I'm having an issue at the moment that I don't understand what is going on as I am very new to Linux.

There is a script that I am trying to run (which I won't post as I don't think I am allowed to) that needs to be run as the root user, but when I run it, it says that it doesn't have the required environment variable and it needs to be set. But the script should do this.

We have it installed on two boxes, one works fine and the other doesn't. When we run this command on the working box:
su - user -l -c env 2>/dev/null | grep 'ENVVARIABLE=' | awk -F= '{print $2}'
It returns the correct path.

If I run it on the other box it brings back nothing. However if I switch the -l and -l around it brings back the correct path.

Can anybody point me in the right direction as to what is going wrong here? I'm not allowed to just upgrade everything on box to see if that fixes the issue unfortunately. But if I can narrow it down that would help a lot as at the moment I don't have any clue. If you need more information then hopefully I can provide (Although that may have to be next week).

Note: In the actual script the -l isn't there at all:
if [ "$myself" = "root" ]; then
ENVVARIABLE=`su - $user -c env 2>/dev/null | grep 'ENVVARIABLE=' | awk -F= '{print $2}'

Edit 1: The same shell (bash) is used for the users on both boxes

  • Does the user have the same default shell on each box? Mar 29 '18 at 10:24
  • Yes, the shell is bash on root and the user for both boxes
    – DanMog
    Mar 29 '18 at 10:55
  • How is ENVVARIABLE being set for user? Is it in ~/.bashrc or something? Is this the same on both hosts?
    – phemmer
    Mar 29 '18 at 12:50

Caveat: su on Linux might not be the same on all systems. As usual, man su should give you answers about your options and version.

Conditions to get an environment propagated:

  1. The variable is exported via command export ENVVARIABLE
  2. su is told to keep the environment, which here (Debian 9) can be done with options -m, -p, --preserve-environment .

So this works here:

su -m - user -c env 2>/dev/null | grep 'ENVVARIABLE=' | awk -F= '{print $2}'

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.