如何给su当前的用户环境变量

时间:2021-05-23 22:58:31

I have a variable that is set through .bashrc.

我有一个通过。bashrc设置的变量。

In ~/.bashrc:

在~ / . bashrc:

PROJ_HOME=~/Projects/stable

From a bash shell, I'd like to do something like this:

从bash shell中,我想做以下事情:

$ su -l kenneth -c 'echo $PROJ_HOME'

However, when I do this, the expected /home/kenneth/Projects/stable is not printed out.

然而,当我这样做时,预期/home/kenneth/ project /stable不会打印出来。

Any ideas on how I can do this?

有什么办法吗?

4 个解决方案

#1


7  

You need to export the variable. You may not need to use the -m option to su to preserve the environment.

您需要导出变量。您可能不需要使用-m选项来进行su以保护环境。

export PROJ_HOME=~/Projects/stable

#2


11  

Have you tried the option su -m ?

你试过su -m选项吗?

-m, --preserve-environment
              do not reset environment variables

For example: su -m kenneth -c 'echo $PROJ_HOME'

例如:su -m kenneth -c 'echo $PROJ_HOME'

#3


6  

Try with su -m -l kenneth -c 'echo $PROJ_HOME'. -m should preserve the environment.

尝试使用su -m -l -l -c 'echo $PROJ_HOME'。-m应该保护环境。

EDIT Reading your question one more time, I think I might understood it reversed. You might also try this: su -l kenneth -c '. /home/kenneth/.bashrc; echo $PROJ_HOME.

再读一遍你的问题,我想我可能理解错了。你也可以试试这个:su -l kenneth -c '。/home/kenneth/.bashrc;回声PROJ_HOME美元。

#4


-1  

Use single quotes around the command:

在命令周围使用单引号:

$ su -l kenneth -c 'echo $PROJ_PATH'

Double quotes interprets the value of $PROJ_PATH as seen by root (empty string), then executes the command "echo (empty string)" as the user kenneth.

双引号解释了root(空字符串)看到的$PROJ_PATH的值,然后作为用户kenneth执行命令“echo(空字符串)”。

Single quotes will pass 'echo $PROJ_PATH' as the command, and the value of $PROJ_PATH in kenneth's environment is what will be echoed.

单引号将传递“echo $PROJ_PATH”作为命令,而在kenneth的环境中$PROJ_PATH的值将会得到响应。

#1


7  

You need to export the variable. You may not need to use the -m option to su to preserve the environment.

您需要导出变量。您可能不需要使用-m选项来进行su以保护环境。

export PROJ_HOME=~/Projects/stable

#2


11  

Have you tried the option su -m ?

你试过su -m选项吗?

-m, --preserve-environment
              do not reset environment variables

For example: su -m kenneth -c 'echo $PROJ_HOME'

例如:su -m kenneth -c 'echo $PROJ_HOME'

#3


6  

Try with su -m -l kenneth -c 'echo $PROJ_HOME'. -m should preserve the environment.

尝试使用su -m -l -l -c 'echo $PROJ_HOME'。-m应该保护环境。

EDIT Reading your question one more time, I think I might understood it reversed. You might also try this: su -l kenneth -c '. /home/kenneth/.bashrc; echo $PROJ_HOME.

再读一遍你的问题,我想我可能理解错了。你也可以试试这个:su -l kenneth -c '。/home/kenneth/.bashrc;回声PROJ_HOME美元。

#4


-1  

Use single quotes around the command:

在命令周围使用单引号:

$ su -l kenneth -c 'echo $PROJ_PATH'

Double quotes interprets the value of $PROJ_PATH as seen by root (empty string), then executes the command "echo (empty string)" as the user kenneth.

双引号解释了root(空字符串)看到的$PROJ_PATH的值,然后作为用户kenneth执行命令“echo(空字符串)”。

Single quotes will pass 'echo $PROJ_PATH' as the command, and the value of $PROJ_PATH in kenneth's environment is what will be echoed.

单引号将传递“echo $PROJ_PATH”作为命令,而在kenneth的环境中$PROJ_PATH的值将会得到响应。