简体   繁体   中英

Passing shell variables to system user

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)

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM