PSk*_*cik 4 linux ssh terminal bash shell
我在我的每个 bash 配置文件 ( ~/.bashrc, ~/.bash_profile, ~/.profile)前面加上了echo NAME_OF_FILE,即当我在~/.bashrc.
令我感到困惑的是,当我通过 ssh 运行命令时,为什么我得到并指出 ~/.bashrc 被包含在内。例如,如果我这样做:
ssh localhost echo hi
Run Code Online (Sandbox Code Playgroud)
我得到
.bashrc
hi
Run Code Online (Sandbox Code Playgroud)
为什么~/.bashrc在这种情况下采购?它不应该被引入,因为它应该运行一个非交互式 bash 会话吗?
事实上,ssh localhost tty让我得到一个“不是 tty”(前面是“.bashrc”,表明它~/.bashrc仍然来自)。
我已经将grep所有配置文件都~/.bashrc明确地用于命令采购,但没有人对此进行解释。
(我只有tty -s && shopt -q login_shell && [[ -r ~/.bashrc ]] && . ~/.bashrc在我的文件中,.bash_profile所以即使在交互式登录 shell 中我也能得到 '.bashrc',但这并不能解释 ssh 问题——我可以将其注释掉,我仍然得到与上述 ssh 示例相同的行为)
我该如何调试?
从bash手册页:
Bash attempts to determine when it is being run with its standard input
connected to a a network con?nection, as if by the remote shell daemon,
usually rshd, or the secure shell daemon sshd. If bash
determines it is being run in this fashion, it reads and executes commands
from ~/.bashrc, if that file exists and is readable.
Run Code Online (Sandbox Code Playgroud)
即,~/.bashrc当您通过 调用它时都会运行ssh,无论您是否有 tty。
如果您只想在.bashrc交互时运行,请在顶部尝试此操作:
# If not running interactively, don't do anything
[[ "$-" != *i* ]] && return
Run Code Online (Sandbox Code Playgroud)
如果这不起作用,请尝试以下操作:
[ -z "$PS1" ] && return
Run Code Online (Sandbox Code Playgroud)