注销并重新登录后,向 .profile 添加功能在终端中不可用

注销并重新登录后,向 .profile 添加功能在终端中不可用

所以我的.profile看起来像这样:

if [ -n "$BASH_VERSION" ]; then
    # include .bashrc if it exists
    if [ -f "$HOME/.bashrc" ]; then
        . "$HOME/.bashrc"
    fi
fi

if [ -f "$HOME/.local/share/profile" ]; then
    . "$HOME/.local/share/profile"
fi


# set PATH so it includes user's private bin directories
PATH="$HOME/bin:$HOME/.local/bin:$PATH"
export VISUAL=nano
export EDITOR="$VISUAL"
stty -ixon

function test-func {
        echo test-func
}

alias test-alias='echo test-alias'

当我登录 Gnome(在 Fedora 35 上)并打开终端模拟器时,我无法执行该函数test-func

$ test-func
bash: test-func: command not found

但是,当我将该函数放入.bashrc文件中并重新注销并重新登录时,我就可以执行该test-func函数。

.profile添加的功能在环境中不可用是否有原因?

编辑:所以我注意到,当我使用非图形登录(使用 Ctrl + Alt + F3 技巧)登录时,该test-func功能可用。 Gnome 没有在.profile非登录 shell中提供功能是否有原因?

编辑 2:我还注意到,对函数使用符合 POSIX 标准的语法(func-name () { ... }而不是function func-name { ... })没有效果。

答案1

这可能更像是一条评论,但由于我没有足够的声誉,以下是我得到的:

  • 这个问题与 Gnome 没有特别关系,我使用的是 KDE,我也遇到了这个问题;
  • 如果我将函数放入~/.bash_profile,它将可用。你能否证实同样的情况?如果您也遇到这种情况,那么我们还有另一条线索:Fedora对非登录 shell 的处理方式有所不同~/.profile~/.bash_profile

它实际上在登录 shell 方面以不同的方式对待它们:如果您使用 X 登录,则~/.profile首先获取来源,甚至在 之前~/.bash_profile,但是当没有 GUI 时,仅~/.bash_profile获取来源(如手册描述)。您可以运行journalctl来查看它/etc/X11/xinit/Xsession初始化了 GUI 会话,以及该Xsession脚本源/etc/X11/xinit/xinitrc-common似乎仅存在于 Fedora 上,而该脚本源又源~/.profile.

我的两分钱。

相关内容