Bash 未在 AIX 中读取(源).bashrc

Bash 未在 AIX 中读取(源).bashrc

为什么 bash 不通过 ssh 在非交互式 shell 中读取 AIX 中的 ~/.bashrc ?根据 bash 手册页https://www.gnu.org/software/bash/manual/bash.html它应该这样做:

由远程 shell 守护进程调用

Bash 尝试确定它何时使用连接到网络连接的标准输入运行,例如何时由远程 shell 守护程序(通常是 rshd)或安全 shell 守护程序 sshd 执行。如果 Bash 确定它正在以这种方式运行,它会从 ~/.bashrc 读取并执行命令,如果该文件存在并且可读。如果作为 sh 调用,则不会执行此操作。 --norc 选项可用于禁止此行为,而 --rcfile 选项可用于强制读取另一个文件,但 rshd 和 sshd 通常都不会使用这些选项调用 shell 或允许指定它们。

一旦在 AIX 7.1 中运行,在 AIX 5.1 中也需要它。

AIX版本:uname -a

AIX p740 1 7????????????电力计算机AIX

bash版本:bash --版本

GNU bash,版本 4.3.42(1)-release (x86_64-pc-linux-gnu) 版权所有 (C) 2013 Free Software Foundation, Inc. 许可证 GPLv3+:GNU GPL 版本 3 或更高版本 http://gnu.org/licenses/gpl.html ...

文件片段:/etc/environment、/etc/profile、.bash_profile 和 .bashrc

/etc/环境

...
TESTGLOBAL="Defined in /etc/environment"
...

/etc/配置文件

# First line
echo "Loading /etc/profile..."
...

.bash_配置文件

# First line
echo "Loading .bash_profile..."
[ -e ~/.bashrc ] && . ~/.bashrc
...

.bashrc

# First line
echo "Loading .bashrc..."
export TESTLOCAL="Defined in ~/.bashrc"
...

远程 shell 是 bash:但它不读取 ~/.bashrc

$ ssh -t user@localhost 'echo $SHELL, $0'
/bin/bash, bash

交互式外壳:

$ ssh user@localhost
*******************************************************************************
*                                                                             *
*                                                                             *
*  Welcome to AIX Version 7.1!                                                *
*                                                                             *
*                                                                             *
*  Please see the README file in /usr/lpp/bos for information pertinent to    *
*  this release of the AIX Operating System.                                  *
*                                                                             *
*                                                                             *
*******************************************************************************
Loading /etc/profile...
Loading .bash_profile...
Loading .bashrc...
$
$ set | grep TEST
TESTLOCAL='Defined in .bashrc'
TESTGLOBAL='"Defined in /etc/environment"'

远程命令:带或不带 -t 标志

$ ssh -t user@localhost 'echo $TESTLOCAL'

$ ssh -t user@localhost 'echo $TESTGLOBAL'
"Defined in /etc/environment"

$ ssh -t user@localhost 'set | grep TEST'
BASH_EXECUTION_STRING='set | grep TEST'
TESTGLOBAL='"Defined in /etc/environment"'

答案1

非交互式 shell 根本不提供源代码.bashrc;这是 的定义行为bash。非交互式 shell 只会获取由BASH_ENV环境变量命名的文件。 (如果运行为sh,那么它会使用ENV它来命名源文件。)

来自手册页(粗体是我的重点)

当 bash 作为交互式登录 shell,或者作为带有 --login 选项的非交互式 shell,它首先从文件中读取并执行命令/etc/配置文件,如果该文件存在。读取该文件后,它会查找~/.bash_profile、~/.bash_login 和 ~/.profile 按此顺序读取并执行第一个命令存在并且可读。启动 shell 时可以使用 --noprofile 选项来禁止此行为。

当登录 shell 退出时,bash 会从文件 ~/.bash_logout(如果存在)中读取并执行命令。

非登录 shell 的交互式 shell 启动后,bash 读取并执行命令~/.bashrc,如果该文件存在。这可以通过使用 --norc 选项来禁止。 --rcfile 文件选项将强制 bash 从文件而不是 ~/.bashrc 读取和执行命令。

当 bash 启动时非交互地,例如,要运行 shell 脚本,它会在环境中查找变量 BASH_ENV,如果它出现在其中,则扩展其值,并使用扩展后的值作为要读取和执行的文件的名称。 Bash 的行为就像执行了以下命令: if [ -n "$BASH_ENV" ];然后 。 “$BASH_ENV”; fi 但 PATH 变量的值不用于搜索文件名。

相关内容