大网数据广告活动
服务器运维问答

sourcing environment variable in bashrc: directly vs export from file yields different result - why

· 52CE 我爱测

I am running systemd via user (i.e. enable-linger $USER ) and I interact with services via systemctl --user I noticed a strange issue. Sourcing Type 1 In order for the above systemd to work for a user, I need to add export XDG_RU…

问题原文

原帖链接 · Newskooler · CC BY-SA 4.0

I am running systemd via user (i.e. enable-linger $USER) and I interact with services via systemctl --user

I noticed a strange issue.

Sourcing Type 1

In order for the above systemd to work for a user, I need to add export XDG_RUNTIME_DIR=/run/user/$(id -u) inside my ~/.bashrc

This works fine.

Sourcing Type 2

When I instead source my bashrc as follows, the systemd does not work:

inside .bashrc:

source /path_to_file/my_file.env

inside my_file.env:

XDG_RUNTIME_DIR=/run/user/$(id -u)

Both Sourcing Type 1 and 2 yield the same result when I do echo $XDG_RUNTIME_DIR so I decided to go with Type 2. However then I noticed that when I do systemctl --user daemon-reload the daemon was not running and I got the following error:

Failed to connect to bus: No such file or directory

When I revert back to Souring Type 1, the error is gone and all works as expected.

My question is: What is happening that I am missing here? My main confusion stems from the fact that the env variables are the same, but the end results are not.

已采纳解答

原帖链接 · u1686_grawity · CC BY-SA 4.0

You forgot the export in your .env file, so the variable isn't actually an environment variable.

Shell variables are not exported to environment by default. Only assignments to an already exported variable will be re-exported automatically; if XDG_RUNTIME_DIR is a brand new variable, it will become a shell internal variable unless you use the export or declare -x builtins. (Or unless the "allexport" shell option is enabled, but using it in .bashrc has some weird gotchas.)

echo $FOO "sees" the variable because the expansion of $FOO in command-line is done by the shell interpreter itself, not by the command – 'echo' doesn't have to access the environment at all, whereas 'systemctl' does.

Instead of echo, you could use declare -p FOO to have the shell itself tell you the variable's state (including showing the x flag if the variable is exported), or use env or printenv to see what the environment looks like from the point of view of an external process (non-exported shell variables simply won't show up here).

Either way, you should never need to do this for interactive logins. This specific variable is supposed to be put in your environment through PAM (by pam_systemd) as soon as you log in – even before your shell is run.

For scripts that need to access another user's services, systemctl --user -M ${USER}@.host will make systemctl itself find the specified user's runtime directory and the D-Bus socket.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2023-03-21)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。