Linux 你如何给 su 当前用户环境变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10488758/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you give su the current user environment variables
提问by kenneth koontz
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/Projects/stable
不会打印出来。
Any ideas on how I can do this?
关于我如何做到这一点的任何想法?
采纳答案by Paused until further notice.
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
回答by Carlo
Try with su -m -l kenneth -c 'echo $PROJ_HOME'
. -m should preserve the environment.
尝试使用su -m -l kenneth -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; echo $PROJ_HOME
。
回答by Dan V
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'
回答by Earl Ruby
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.
双引号解释$PROJ_PATH
root 看到的值(空字符串),然后"echo (empty string)"
以用户 kenneth 的身份执行命令。
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'
作为命令传递,$PROJ_PATH
在 kenneth 的环境中的值将被回显。