CentOS 7.9 中 /etc/profile.d 中的哪些文件来源顺序如何? (用于定义别名)

CentOS 7.9 中 /etc/profile.d 中的哪些文件来源顺序如何? (用于定义别名)

我有一个与 CentOS 7.9 上的别名/环境变量相关的问题。我正在尝试为 ll 创建一个别名:

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

然而,在我这样做并重新启动控制台后(我使用的是新的 DigitalOcean CentOS 7.9 Droplet 及其 Droplet 控制台),当我执行“type ll”时,它会显示:

ll is aliased to `ls -l --color=auto'

当我使用 grep 在文件夹中搜索字符串“alias ll”时:

grep -rnw /etc/profile.d -e 'alias ll'

它给了我另外两个文件,colorls.sh 和 colorls.csh:

/etc/profile.d/Z-alias-ll.sh:1:alias ll="ls -alhF --color=auto"
/etc/profile.d/colorls.csh:13:alias ll 'ls -l'
/etc/profile.d/colorls.csh:66:alias ll 'ls -l --color=auto'
/etc/profile.d/colorls.sh:9:  alias ll='ls -l' 2>/dev/null
/etc/profile.d/colorls.sh:55:alias ll='ls -l --color=auto' 2>/dev/null

如果我移动或删除这些文件,那么我的别名就可以使用。所以我猜这些文件正在覆盖我的别名。但我想,既然我在文件名前面加上了 Z- 前缀,那么后者会覆盖前者,因为我认为它会稍后被获取,Z* 在 c* 之后,因为这就是它在 Ubuntu 中的工作方式。

我想也许我是在倒退,所以我也这样做了:

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

但是当我重新启动控制台时,“type ll”仍然显示“ll 是 `ls -l --color=auto' 的别名”。

为了测试哪个文件名最终会被最后获取,我做了:

echo 'alias testalias="ls -a"' > /etc/profile.d/0-alias-testalias.sh
echo 'alias testalias="ls -lh"' > /etc/profile.d/alias-testalias.sh
echo 'alias testalias="ls -F"' > /etc/profile.d/Z-alias-testalias.sh

重新启动后,“输入 testalias”会显示“testalias 的别名为‘ls -lh’”。所以最后获取的是文件alias-testalias.sh。但是,我不明白为什么会这样,因为按字母顺序它位于其他两者之间。

我的 /root/.bashrc 包含:

# .bashrc

# User specific aliases and functions

alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'

# Source global definitions
if [ -f /etc/bashrc ]; then
        . /etc/bashrc
fi

我不确定,但我认为 /etc/bashrc 的这一部分正在获取 /etc/profile.d 文件:

SHELL=/bin/bash
# Only display echos from profile.d scripts if we are no login shell
# and interactive - otherwise just process them to set envvars
for i in /etc/profile.d/*.sh; do
    if [ -r "$i" ]; then
        if [ "$PS1" ]; then
            . "$i"
        else
            . "$i" >/dev/null
        fi
    fi
done

我创建了一个文件 sort.sh :

for i in /etc/profile.d/*.sh; do
     echo $i
done

然后执行“bash sort.sh”,这给了我:

/etc/profile.d/0-alias-ll.sh
/etc/profile.d/0-alias-testalias.sh
/etc/profile.d/256term.sh
/etc/profile.d/-alias-ll.sh
/etc/profile.d/alias-ll.sh
/etc/profile.d/alias-testalias.sh
/etc/profile.d/colorgrep.sh
/etc/profile.d/colorls.sh
/etc/profile.d/lang.sh
/etc/profile.d/less.sh
/etc/profile.d/which2.sh
/etc/profile.d/Z-alias-ll.sh
/etc/profile.d/Z-alias-testalias.sh

colorls.sh 位于我尝试定义别名 ll="ls -alhF --color=auto" 的其他文件之间。

这里的采购订单如何运作?为什么文件名“colorls.sh”源自 0-alias-ll.sh、-alias-ll.sh、alias-ll.sh 和 Z-alias-ll.sh 之后?尽管按字母顺序它位于中间,而且我的 sort.sh 文件也将其排序在中间。

可能是 /etc/bashrc 中的“if”条件?我不确定这些条件到底是做什么的,或者是否是其他条件而不是 if 条件。

相关内容