Reputation: 12593
Let's say I want to run some command as system user (no shell):
su -s /bin/bash -c "some_command $TEST" my_system_user
How di I pass $TEST variable to user?
I have the variable configured for all users in /etc/profile.d/my_vars.sh
export TEST=test_arg
But that doesn't get loaded for the above scenario...
I have tried with no luck:
-m (--preserve-environment)
Upvotes: 2
Views: 155
Reputation: 8241
First, I tried to create such a file as /etc/profile.d/my_vars.sh
and I put a test variable in the file. I can see the value of the variable, no matter I run command as whoever. I've also tried to set a variable on-the-fly with the following code, it works on my centos:
unset A_VAR; export A_VAR=foo
su -s /bin/bash -c "echo val=$A_VAR" my_system_user
> val=foo
unset A_VAR
An alternative solution if it does not work for you, you can consider of playing a ssh
trick.
unset A_VAR; export A_VAR=foo
ssh -o SendEnv my_system_user@localhost "echo val=$A_VAR"
> val=foo
unset A_VAR
Upvotes: 3
Reputation: 1245
That's weird. It seems to work for me. I can do:
$ export TEST="hello"
$ su -s /bin/bash -c "echo $TEST"
password:
hello
...I guess I'm missing something about the question.
Upvotes: 0