bash ~/.profile、~/.bashrc 和 ~/.bash_profile 未在新终端启动时运行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25025799/
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-08 21:36:01  来源:igfitidea点击:

~/.profile, ~/.bashrc, and ~/.bash_profile not running on new terminal start up

macosbashterminal.bash-profile

提问by DazedAndConfused

I am trying to create a permanent alias for my terminal. I put the alias in my ~/.profile, ~/.bashrc, and ~/.bash_profile files, previously empty. When I start a new terminal, bash does not recognize the alias, but if I source any of them, it does. Why are these not getting run when I open a terminal? I am on OSX.

我正在尝试为我的终端创建一个永久别名。我将别名放在了之前为空的 ~/.profile、~/.bashrc 和 ~/.bash_profile 文件中。当我启动一个新终端时,bash 无法识别别名,但是如果我找到其中的任何一个,它就会识别。为什么当我打开终端时这些没有运行?我在 OSX 上。

回答by ptierno

Two things need to happen here when using iTermto get the loading of dotfiles to work.

当使用iTermdotfiles 加载工作时,这里需要做两件事。

First you should add the following to your .bash_profile

首先,您应该将以下内容添加到您的 .bash_profile

[[ -s ~/.bashrc ]] && source ~/.bashrc

Secondly you will need to ensure that in iTermpreferences your terminal is set to launch a login shell.

其次,您需要确保在iTerm首选项中您的终端设置为启动登录 shell。

iTerm Preferences

iTerm 偏好设置

Hope this helps!

希望这可以帮助!

回答by Tony Ly

Using the default mac terminal, what worked for me was to add a command to run on start up to source my .bash_profile.

使用默认的 mac 终端,对我有用的是添加一个命令以在启动时运行以获取我的 .bash_profile。

Preferences > Profile > Startup > Add command 'source ~/.bash_profile'

首选项> 配置文件> 启动> 添加命令'source ~/.bash_profile'

Mac terminal preferences window screenshot

Mac 终端首选项窗口截图

Might be considered to be a bit hacky, but it does the trick.

可能被认为有点hacky,但它确实有效。

回答by Cafebabe

Adding source ~/.profileto my .bash_profileworked for me.

添加source ~/.profile到我.bash_profile对我来说有效。

回答by Randy

As of High Sierra, both Terminal and iTerm want to load ~/.profile first. So I suggest you put one line in your .profile to make your Mac work like other Unixes:

从 High Sierra 开始,Terminal 和 iTerm 都希望首先加载 ~/.profile。所以我建议你在你的 .profile 中加入一行,让你的 Mac 像其他 Unix 一样工作:

source ~/.bash_profile

源 ~/.bash_profile

By editing this one file, you won't have to search through the menus of multiple apps to override Apple's bizarre behavior in each.

通过编辑这个文件,您不必搜索多个应用程序的菜单来覆盖每个应用程序中Apple 的怪异行为。

回答by l3x

Why are your shell's initialization files not loading?

为什么你的 shell 的初始化文件没有加载?

As with most things, It Depends ?

与大多数事情一样,这取决于?

I recently experienced the same phenomenon and went through the following exercise to resolve it:

我最近遇到了同样的现象,并通过以下练习来解决它:

I use iTerm. iTerm runs a login shell by default. Verify in iTerm Preferences > General > Command > (*) Login Shell Therefore, I know that ~/.bash_profile will always be called.

我使用 iTerm。iTerm 默认运行登录 shell。在 iTerm Preferences > General > Command > (*) Login Shell 中验证因此,我知道 ~/.bash_profile 将始终被调用。

Knowing that, I put the following in my ~/.bash_profile file:

知道了这一点,我将以下内容放入我的 ~/.bash_profile 文件中:

for file in ~/.{bashrc,bash_exports,bash_aliases,bash_functions}; do
    [ -r "$file" ] && source "$file"
done
unset file

Notice that I use separate files for .bashrc, .bash_exports, etc. It keeps things separate and simple.

请注意,我为 .bashrc、.bash_exports 等使用了单独的文件。它使事情保持独立和简单。

Note also that /etc/profile is loaded first, but since I have never used that system wide init file, I knew that that was not my problem. For details check out $ man bash

另请注意,首先加载 /etc/profile,但由于我从未使用过该系统范围的 init 文件,所以我知道这不是我的问题。详情请查看$ man bash

So, I started with my ~/.bash_profile file.

所以,我从我的 ~/.bash_profile 文件开始。

I found that when I installed Canopy Expressthat it's installer replaced the contents of my ~/.bash_profile file with the following content:

我发现当我安装Canopy Express 时,它的安装程序将我的 ~/.bash_profile 文件的内容替换为以下内容:

# Added by Canopy installer on 2017-04-19
# VIRTUAL_ENV_DISABLE_PROMPT can be set to '' to make the bash prompt show that Canopy is active, otherwise 1
alias activate_canopy="source '/Users/lex/dev/python/User/bin/activate'"
# VIRTUAL_ENV_DISABLE_PROMPT=1 source '/Users/lex/dev/python/User/bin/activate'

p.s. Canopy is an excellent, free python IDE, that I highly recommend.

ps Canopy 是一款出色的免费 Python IDE,我强烈推荐。

Fortunately, I backup my ~/.bash* files so restoring that was easy and quickly fixed my issue.

幸运的是,我备份了我的 ~/.bash* 文件,因此恢复它很容易并快速解决了我的问题。

My advice would be to understand the order of calls to your initialization files and start with the first one and work your way through them until you find the problem.

我的建议是了解对初始化文件的调用顺序,并从第一个开始,然后逐步完成,直到找到问题为止。

Oh, and you may want to verify which shell you are using (I use bash):

哦,您可能想验证您使用的是哪个 shell(我使用 bash):

~ $ echo $SHELL
/usr/local/bin/bash

回答by Cross

I am guessing you may use another shell, such as bash, tcsh, sh, zsh etc.

我猜你可能会使用另一个 shell,比如 bash、tcsh、sh、zsh 等。

Put source .bash_profileinto your appropriate 'bashrc' file will make the auto loading recovered, i.e.

放入source .bash_profile适当的“bashrc”文件将使自动加载恢复,即

.login for tcsh, .bash_profile for bash, .zshrc for zsh

.login 用于 tcsh,.bash_profile 用于 bash,.zshrc 用于 zsh

回答by braitsch

As of Catalina the default shell is now zsh. You can change it back to bash with chsh -s /bin/bashand that should load your .profileor .bash_profile

从 Catalina 开始,默认 shell 现在是 zsh。您可以将其改回 bash withchsh -s /bin/bash并且应该加载您的.profile.bash_profile