什么会覆盖 /etc/profile.d 中的文件创建的别名?(环境变量)

什么会覆盖 /etc/profile.d 中的文件创建的别名?(环境变量)

我在 CentOS 7.9 中执行了以下命令来创建别名文件:

echo 'alias ll="ls -alhF --color=auto"' > /etc/profile.d/alias-ll.sh

然后,我重新启动 shell 后,“type ll” 表示该别名不起作用。我想这可能是由于 /etc/profile.d 中的另一个文件覆盖了别名,所以我将文件重命名为“z-alias-ll.sh”。然后我重新启动 shell,这一次,“type ll” 表示别名已成功运行。

但是当我在 Ubuntu 20.04 中执行相同操作时,“type ll”表示它不起作用。Ubuntu 中的 /etc/profile.d 中有一些文件名,如“Z99-cloud-locale-test.sh”和“Z99-cloudinit-warnings.sh”,因此我尝试了这些:

echo 'alias ll="ls -alhF --color=auto"' > /etc/profile.d/Z99-alias-ll.sh
echo 'alias ll="ls -alhF --color=auto"' > /etc/profile.d/ZZ99-alias-ll.sh
echo 'alias ll="ls -alhF --color=auto"' > /etc/profile.d/Z99-z-alias-ll.sh
echo 'alias ll="ls -alhF --color=auto"' > /etc/profile.d/ZZ99-z-alias-ll.sh
echo 'alias ll="ls -alhF --color=auto"' > /etc/profile.d/Z99Zalias-ll.sh

但是,当我重新启动终端时,“type ll”仍然显示“ll 的别名为‘ls -alF’”。

如果我创建另一个别名文件:

echo 'alias lltest="ls -alhF --color=auto"' > /etc/profile.d/alias-lltest.sh

然后,在我重新启动终端后,“type lltest”表示正在获取此文件。因此,我认为正在获取 /etc/profile.d 中的别名文件,但“ll”的别名正在被覆盖。

由于我尝试过在文件名前加上“ZZ99”等前缀,因此我认为这不是字符串排序问题,就像 CentOS 7.9 那样,我必须在文件名前加上“z-”前缀,以防止另一个 /etc/profile.d 文件优先。但是,我并不是 100% 确定。

哪些文件、脚本等可能优先于我在 /etc/profile.d 中创建的别名文件?在定义环境变量方面,Ubuntu 中哪些文件优先于 /etc/profile.d?

我尝试在 Ubuntu 20.04 和 Ubuntu 22.10 上使用新鲜的 Digital Ocean droplet。

答案1

通常,用户设置优先于系统范围设置。这也适用于别名。用户定义的别名优先于系统级别定义的别名。默认情况下,在 Ubuntu 上,alias ll='ls -alF'在用户的文件中定义.bashrc,从而覆盖您可能在其他地方设置的任何别名。

相关内容