xterm 生成的 shell 中的 $PATH 不包含命令位置,但 shell 仍然能够执行它

xterm 生成的 shell 中的 $PATH 不包含命令位置,但 shell 仍然能够执行它

我正在使用 Fluxbox 窗口管理器设置 Debian 10。我需要的组件之一是 miniconda,我已经安装了最初的 bash(无桌面)中的所有内容,并且一切正常。 Miniconda 安装会更改 /.bashrc 文件,将以下几行附加到其内容中:

# >>> conda initialize >>>
# !! Contents within this block are managed by 'conda init' !!
__conda_setup="$('/opt/miniconda3/bin/conda' 'shell.bash' 'hook' 2> /dev/null)"
if [ $? -eq 0 ]; then
    eval "$__conda_setup"
else
    if [ -f "/opt/miniconda3/etc/profile.d/conda.sh" ]; then
        . "/opt/miniconda3/etc/profile.d/conda.sh"
    else
        export PATH="/opt/miniconda3/bin:$PATH"
    fi
fi
unset __conda_setup
# <<< conda initialize <<<

它工作正常,初始(无 wm)bash 正确设置 $PATH,可以毫无问题地运行 conda,并echo $PATH在重新登录后相应地显示包含的 conda 安装目录。

然而,对于在 Fluxbox 下使用 xterm 创建的终端,我发现了一种我会描述为奇怪的行为:

1)尝试使用 'conda' 查找which没有给出答案;

2)echo $PATH不显示conda安装目录;

3)然而,conda 仍然有效,我可以conda update conda毫无问题地做到这一点;

4) 之后,conda 安装目录仍然没有添加到 $PATH 中,并且which仍然无法找到它;

5) 请求 conda 环境激活,egconda activate base工作正常,这样做可以使 shell 正确更新 $PATH。

我只是想知道为什么会这样。在这个过程中有一些我不太理解的地方:在这种情况下,环境变量到底是如何传递到 xterm 创建的 shell 的?为什么在 $PATH 中找不到conda它却能够运行?which

===编辑=== 包括所需的输出:

root@Zumbi:~# type conda > type_conda_out.txt

conda is a function
conda () 
{ 
    if [ "$#" -lt 1 ]; then
        "$CONDA_EXE" $_CE_M $_CE_CONDA;
    else
        \local cmd="$1";
        shift;
        case "$cmd" in 
            activate | deactivate)
                __conda_activate "$cmd" "$@"
            ;;
            install | update | upgrade | remove | uninstall)
                CONDA_INTERNAL_OLDPATH="${PATH}";
                __add_sys_prefix_to_path;
                "$CONDA_EXE" $_CE_M $_CE_CONDA "$cmd" "$@";
                \local t1=$?;
                PATH="${CONDA_INTERNAL_OLDPATH}";
                if [ $t1 = 0 ]; then
                    __conda_reactivate;
                else
                    return $t1;
                fi
            ;;
            *)
                CONDA_INTERNAL_OLDPATH="${PATH}";
                __add_sys_prefix_to_path;
                "$CONDA_EXE" $_CE_M $_CE_CONDA "$cmd" "$@";
                \local t1=$?;
                PATH="${CONDA_INTERNAL_OLDPATH}";
                return $t1
            ;;
        esac;
    fi
}

root@Zumbi:~# /opt/miniconda3/bin/conda shell.bash hook > hook_out.txt

export CONDA_EXE='/opt/miniconda3/bin/conda'
export _CE_M=''
export _CE_CONDA=''
export CONDA_PYTHON_EXE='/opt/miniconda3/bin/python'

# Copyright (C) 2012 Anaconda, Inc
# SPDX-License-Identifier: BSD-3-Clause

__add_sys_prefix_to_path() {
    # In dev-mode CONDA_EXE is python.exe and on Windows
    # it is in a different relative location to condabin.
    if [ -n "${_CE_CONDA}" ] && [ -n "${WINDIR+x}" ]; then
        SYSP=$(\dirname "${CONDA_EXE}")
    else
        SYSP=$(\dirname "${CONDA_EXE}")
        SYSP=$(\dirname "${SYSP}")
    fi

    if [ -n "${WINDIR+x}" ]; then
        PATH="${SYSP}/bin:${PATH}"
        PATH="${SYSP}/Scripts:${PATH}"
        PATH="${SYSP}/Library/bin:${PATH}"
        PATH="${SYSP}/Library/usr/bin:${PATH}"
        PATH="${SYSP}/Library/mingw-w64/bin:${PATH}"
        PATH="${SYSP}:${PATH}"
    else
        PATH="${SYSP}/bin:${PATH}"
    fi
    \export PATH
}

__conda_hashr() {
    if [ -n "${ZSH_VERSION:+x}" ]; then
        \rehash
    elif [ -n "${POSH_VERSION:+x}" ]; then
        :  # pass
    else
        \hash -r
    fi
}

__conda_activate() {
    if [ -n "${CONDA_PS1_BACKUP:+x}" ]; then
        # Handle transition from shell activated with conda <= 4.3 to a subsequent activation
        # after conda updated to >= 4.4. See issue #6173.
        PS1="$CONDA_PS1_BACKUP"
        \unset CONDA_PS1_BACKUP
    fi

    \local cmd="$1"
    shift
    \local ask_conda
    CONDA_INTERNAL_OLDPATH="${PATH}"
    __add_sys_prefix_to_path
    ask_conda="$(PS1="$PS1" "$CONDA_EXE" $_CE_M $_CE_CONDA shell.posix "$cmd" "$@")" || \return $?
    rc=$?
    PATH="${CONDA_INTERNAL_OLDPATH}"
    \eval "$ask_conda"
    if [ $rc != 0 ]; then
        \export PATH
    fi
    __conda_hashr
}

__conda_reactivate() {
    \local ask_conda
    CONDA_INTERNAL_OLDPATH="${PATH}"
    __add_sys_prefix_to_path
    ask_conda="$(PS1="$PS1" "$CONDA_EXE" $_CE_M $_CE_CONDA shell.posix reactivate)" || \return $?
    PATH="${CONDA_INTERNAL_OLDPATH}"
    \eval "$ask_conda"
    __conda_hashr
}

conda() {
    if [ "$#" -lt 1 ]; then
        "$CONDA_EXE" $_CE_M $_CE_CONDA
    else
        \local cmd="$1"
        shift
        case "$cmd" in
            activate|deactivate)
                __conda_activate "$cmd" "$@"
                ;;
            install|update|upgrade|remove|uninstall)
                CONDA_INTERNAL_OLDPATH="${PATH}"
                __add_sys_prefix_to_path
                "$CONDA_EXE" $_CE_M $_CE_CONDA "$cmd" "$@"
                \local t1=$?
                PATH="${CONDA_INTERNAL_OLDPATH}"
                if [ $t1 = 0 ]; then
                    __conda_reactivate
                else
                    return $t1
                fi
                ;;
            *)
                CONDA_INTERNAL_OLDPATH="${PATH}"
                __add_sys_prefix_to_path
                "$CONDA_EXE" $_CE_M $_CE_CONDA "$cmd" "$@"
                \local t1=$?
                PATH="${CONDA_INTERNAL_OLDPATH}"
                return $t1
                ;;
        esac
    fi
}

if [ -z "${CONDA_SHLVL+x}" ]; then
    \export CONDA_SHLVL=0
    # In dev-mode CONDA_EXE is python.exe and on Windows
    # it is in a different relative location to condabin.
    if [ -n "${_CE_CONDA+x}" ] && [ -n "${WINDIR+x}" ]; then
        PATH="$(\dirname "$CONDA_EXE")/condabin${PATH:+":${PATH}"}"
    else
        PATH="$(\dirname "$(\dirname "$CONDA_EXE")")/condabin${PATH:+":${PATH}"}"
    fi
    \export PATH

    # We're not allowing PS1 to be unbound. It must at least be set.
    # However, we're not exporting it, which can cause problems when starting a second shell
    # via a first shell (i.e. starting zsh from bash).
    if [ -z "${PS1+x}" ]; then
        PS1=
    fi
fi
root@Zumbi:~# type -f conda
bash: type: conda: not found

===编辑2=== 现在,在编辑FB的菜单时,我可以看到如何调用xterm:它是由包装器完成的: x-terminal-emulator -T "Bash" -e /bin/bash --login--login选项,如中所述bash 文档,

使 bash 的行为就像它被作为登录 shell 调用一样

所以它解释了为什么 bash 没有获取 $PATH。在 bash 中使用fbrun xterm结果,并按预期初始化系统 $PATH。

答案1

运行该命令conda是有效的,因为它在 shell 初始化期间被定义为 shell 函数。该函数包含二进制文件的完整路径,因此无需在 中查找它$PATH

which不显示 shell 函数(默认情况下)。

相关内容