家目录下的 .bashrc 文件应该自动加载吗?

9
我在我的.bashrc文件中添加了scala,但是当我关闭并重新打开我的mac时,它无法找到它。当我输入以下命令时:
source ~/.bashrc 

一切已经恢复正常。我想说问题在整个文件中,但问题是我之前有其他工作正常的内容,但是scala的问题是持久存在的。有人知道原因并能够解释为什么会出现这个问题吗?这是我的.bashrc文件中的内容,可以正确运行rvm和mysql,但无法运行scala:

PATH=$PATH:$HOME/.rvm/bin # Add RVM to PATH for scripting
export PATH="/usr/local/mysql/bin:$PATH"
export PATH="/Users/Zeroe/scala-2.9.1-1/bin:$PATH"

2
请点击这里查看。 - ughoavgfhw
谢谢你提供的链接,我有机会时会尝试一下。如果有效的话,请务必将其作为答案提交,以便我接受。 - Andy
2个回答

55

我通过在这些脚本中添加echo "${BASH_SOURCE[0]}"来弄清楚这个图表。

                     +-----------------+   +------FIRST-------+   +-----------------+
                     |                 |   | ~/.bash_profile  |   |                 |
login shell -------->|  /etc/profile   |-->| ~/.bash_login ------>|  ~/.bashrc      |
                     |                 |   | ~/.profile       |   |                 |
                     +-----------------+   +------------------+   +-----------------+
                     +-----------------+   +-----------------+
                     |                 |   |                 |
interactive shell -->|  ~/.bashrc -------->| /etc/bashrc     |
                     |                 |   |                 |
                     +-----------------+   +-----------------+
                     +-----------------+
                     |                 |
logout shell ------->|  ~/.bash_logout |
                     |                 |
                     +-----------------+

注意

  1. []-->[] 表示通过工作流源码 (自动化)。
  2. [--->[] 表示按照约定源码 (手动操作,否则不会发生任何事情)。
  3. FIRST 表示查找第一个可用的,忽略其余部分

那真是一个非常好的答案。 - Daniel Kamil Kozar
3
漂亮的图片 - 比通常在这些材料中看到的一堵文本墙好得多。 - Michael Burr
2
一些注意事项: "交互式shell" 应该是 "交互式、非登录shell",而 "登录shell" 应该是 "交互式、登录shell"。此外,在Ubuntu上,对于非登录的交互式shell,首先读取 /etc/bash.bashrc,然后是 ~/.bashrc。请注意,/etc/bash.bashrc 不在原始的GNU Bash中,它是许多发行版所做的修改。 - philb

11
你的shell可能是登录shell,如果是这样,bash将按照以下顺序读取各种配置文件:
  1. /etc/profile
  2. ~/.bash_profile
  3. ~/.bash_login
  4. ~/.profile
通常可以从上述任意一文件中调用~/.bashrc以便使得登录shell和非登录shell使用相同的配置。
以下是我的~/.profile内容:
# ~/.profile: executed by the command interpreter for login shells.
# This file is not read by bash(1), if ~/.bash_profile or ~/.bash_login
# exists.
# see /usr/share/doc/bash/examples/startup-files for examples.
# the files are located in the bash-doc package.

# the default umask is set in /etc/profile; for setting the umask
# for ssh logins, install and configure the libpam-umask package.
#umask 022

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

# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/bin" ] ; then
    PATH="$HOME/bin:$PATH"
fi
export LANGUAGE="en_US:en"
export LC_MESSAGES="en_US.UTF-8"
export LC_CTYPE="en_US.UTF-8"
export LC_COLLATE="en_US.UTF-8"

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接