bash 主目录中的 .bashrc 是否应该自动加载?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9953005/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 01:55:31  来源:igfitidea点击:

Should the .bashrc in the home directory load automatically?

macosbash

提问by Andy

I added scala in my .bashrc file, but when I shut off my mac and turn it back on it does not find it. When i do

我在我的 .bashrc 文件中添加了 scala,但是当我关闭我的 mac 并重新打开它时,它没有找到它。当我做

source ~/.bashrc 

all is back to normal. I would say the issue is with the whole file in general, but the problem, is I have other things in there that have worked just fine before, but the problem is persistent with scala. Anybody know why this is and explain why I am getting the problem? This is whats in my .bashrc file, which runs rvm and mysql correctly, but not scala:

一切恢复正常。我会说问题通常出在整个文件上,但问题是我在那里有其他东西以前工作得很好,但问题在 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"

回答by kev

I figured out this diagram by adding echo "${BASH_SOURCE[0]}"to those scripts.

我通过添加echo "${BASH_SOURCE[0]}"到这些脚本中得出了这个图。

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

Note

笔记

  1. []-->[]means source by workflow(Automatically).
  2. [--->[]means source by convention(Manually. If not, nothing happen.).
  3. FIRSTmeans find the first available, ignore rest
  1. []-->[]意味着source by workflow(自动)。
  2. [--->[]意味着source by convention(手动。如果没有,什么都不会发生。)。
  3. FIRST方法 find the first available, ignore rest

回答by A B

Your shell is probably a login shell, in which case bash will read various profile files in order:

您的 shell 可能是一个登录 shell,在这种情况下,bash 将按顺序读取各种配置文件:

  1. /etc/profile
  2. ~/.bash_profile
  3. ~/.bash_login
  4. ~/.profile
  1. /etc/profile
  2. ~/.bash_profile
  3. ~/.bash_login
  4. ~/.profile

It's typical to source ~/.bashrcfrom one of those files so you get the same config for login shells as well.

通常~/.bashrc从这些文件之一中获取源代码,因此您也可以获得相同的登录 shell 配置。

This is what my ~/.profilecontains:

这是我的~/.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"