su user以root身份执行命令


1

背景短:

运行使用的脚本时,notify-send "title" "message"预计会显示通知,但sudo ./script不会显示通知

在阅读并访问了许多页面之后,我发现运行我脚本的这部分(通知 - 发送部分)仍然可以通知用户。

问题:

因此测试这个su命令:

su normal-user -c "echo $USER"
root

为什么不回来 normal-user

Answers:


4

$ USER变量在您输入命令时不会在su运行之后展开。 su normal-user -c 'echo $USER'将产生预期的结果。单引号意味着$ USER在su之后才会扩展。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.