【问题标题】:Reading "bash_profile" is doing two things that are contradicting each other阅读“bash_profile”正在做两件相互矛盾的事情
【发布时间】:2017-02-07 14:09:22
【问题描述】:

我对我的 shell 如何读取 bash_profile 感到非常困惑。

在 root 中,我的 ~/.bash_profile 看起来像这样

# .bash_profile

# Get the aliases and functions
if [-f ~/.bashrc ]; then
        .~/.bashrc
fi

PATH=$PATH:$HOME/bin:$HOME/sbin:$HOME/usr/sbin:$HOME/usr/bin:/usr/sbin
LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib

export PATH=$PATH
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH
unset USERNAME

没有 ~/.profile 文件。

在名为 maruhan 的用户中,我的 ~/.bash_profile 看起来像这样

# .bash_profile

# Get the aliases and functions
if [-f ~/.bashrc ]; then
        .~/.bashrc
fi

PATH=$PATH:$HOME/bin:$HOME/sbin:$HOME/usr/sbin:$HOME/usr/bin:/usr/sbin
LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home/maruhan/Desktop/issac:/usr/local/lib
ASDF=$ASDF:/home

export PATH=$PATH
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH
export ASDF=$ASDF
unset USERNAME

我的 ~/.profile 看起来像这样

LD_LIBRARY_PATH=/home/maruhan/Desktop/issac:/usr/local/lib:$LD_LIBRARY_PATH
ASDF=/home:$ASDF
export ASDF=$ASDF
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH

您可以清楚地看到,ASDF 没有在 root 的 bash_profile 中定义。

但是,当我调用 export 时,我会在 root 中得到这个。

declare -x ASDF=":/home"

但与 LD_LIBRARY_PATH 无关。

奇怪的是,在 maruhan 中,运行导出会同时显示 ASDF 和 LD_LIBRARY_PATH。

此外,/etc/environment 中没有任何关于 ASDF 或 LD_LIBRARY_PATH 的内容。我也没有 /etc/bash_profile 文件。

运行echo $0 给我bash 用于root 和maruhan。

为什么 LD_LIBRARY_PATH 在 ASDF 存在时在 root 中消失了?

【问题讨论】:

  • root 用户如何启动bash?还有maruhan?
  • @whjm 你能帮我检查一下吗?
  • export PATH LD_LIBRARY_PATH ASDF 就足够了;您不需要“重新分配”这些值。 export 标记名称,而不是值。
  • @chepner 我知道。我不是 100% 确定,而且事情没有按预期进行,所以我把它放在那里以防万一。
  • @CuriousKimchi -- 更新了我的答案。

标签: bash shell terminal


【解决方案1】:

规则有点复杂。根据 bash 的手册页:

调用

login shell 是一个参数零的第一个字符是-,或者一个 从--login 选项开始。

交互式 shell 是在没有非选项参数的情况下启动的(除非 -s 是 指定)并且没有 -c 选项,其标准输入和错误都是 连接到终端(由isatty(3) 确定),或以-i 开头的终端 选项。 PS1 已设置,$- 包括 i 如果 bash 是交互式的,则允许 shell 测试此状态的脚本或启动文件。
... ...
当 bash 作为交互式登录 shell 调用时,或作为非交互式 带有--login 选项的shell,它首先从 文件/etc/profile,如果该文件存在。读取该文件后,它会寻找 ~/.bash_profile、~/.bash_login 和 ~/.profile,按此顺序,然后读取和 从存在且可读的第一个命令执行。 --noprofile 启动 shell 时可以使用选项来禁止此行为。
... ...
当一个不是登录 shell 的交互式 shell 启动时,bash 读取并 如果该文件存在,则执行来自~/.bashrc 的命令。这可能会被抑制 使用--norc 选项。 --rcfile 文件选项将强制 bash 读取并 从文件而不是~/.bashrc执行命令。
... ...

请注意,在某些系统上,bash 可能会被定制,以便它在为不是 交互式 shell 的 采购 ~/.bashrc 之前执行系统范围的 rc 文件(例如 /etc/bash.bashrc) >登录外壳。

由登录机制启动的 Shell(通常带有 用户名/密码 提示,如控制台登录、telnet、ssh、...)通常是登录外壳。对于登录 shell,$0 通常是 -bash。

[local] % ssh user@host  <-- The user is trying to login
Password: P@ssw0rd
[remote] % echo $0
-bash                    <-- This is a login shell
[remote] % bash          <-- This is not a login (no username/password)
[remote] % echo $0
bash                     <-- Not a login shell
[remote] %

为了让生活更轻松,我会将所有 rc 内容放在 ~/.bashrc 和 source ~/.bashrc 中 ~/.bash_profile。例如:

% cat ~/.bash_profile
[[ -f ~/.bashrc ]] && source ~/.bashrc
% cat ~/.bashrc
# return immediately if not in an interactive shell
[[ $- != *i* ]] && return 0

export FOO=bar
PATH=$PATH:/my/path
%

【讨论】:

    猜你喜欢
    • 2022-12-04
    • 2014-10-18
    • 1970-01-01
    • 2016-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多