bash 如何在su的输入命令中定义环境变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3676548/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 22:37:04  来源:igfitidea点击:

How to define environment variable in input command of su

bashshellsu

提问by Attila Zobolyak

This command has an empty output.

此命令的输出为空。

su user -c "ABC=abc;echo $ABC"

Any idea, how can I define a variable in the input command?

任何想法,如何在输入命令中定义变量?

采纳答案by Paused until further notice.

Change your quotes to single quotes. The double quotes allow the variable to be substituted in the current environment where it's not set yet. To see the difference, try your version with $USERand compare it to this one:

将引号更改为单引号。双引号允许在尚未设置的当前环境中替换变量。要查看差异,请尝试使用您的版本$USER并将其与此版本进行比较:

su user -c 'ABC=abc; echo $ABC; echo $USER'

回答by William Pursell

If using a bourne shell variant:

如果使用 bourne shell 变体:

ABC=abc su user -c 'echo $ABC'

If not, use env.

如果没有,请使用 env。

env ABC=abc su user -c 'echo $ABC'