简体   繁体   English

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

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

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.我将别名放在了之前为空的 ~/.profile、~/.bashrc 和 ~/.bash_profile 文件中。 When I start a new terminal, bash does not recognize the alias, but if I source any of them, it does.当我启动一个新终端时,bash 无法识别别名,但是如果我找到其中的任何一个,它就会识别。 Why are these not getting run when I open a terminal?为什么当我打开终端时这些没有运行? I am on OSX.我在 OSX 上。

Two things need to happen here when using iTerm to get the loading of dotfiles to work.当使用iTerm来加载 dotfiles 时,这里需要做两件事。

First you should add the following to your .bash_profile首先,您应该将以下内容添加到您的.bash_profile

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

Secondly you will need to ensure that in iTerm preferences your terminal is set to launch a login shell.其次,您需要确保在iTerm首选项中您的终端设置为启动登录 shell。

iTerm 偏好设置

Hope this helps!希望这可以帮助!

Newer MacOS versions use zsh as the default shell for both Terminal and iTerm2.较新的 MacOS 版本使用 zsh 作为终端和 iTerm2 的默认 shell。 Run echo $SHELL to confirm if this is the case for you.运行echo $SHELL以确认您是否属于这种情况。

Zsh looks for a .zshrc file upon shell startup, so to continue using zsh while sourcing the contents of your bash profile, you can run the following: Zsh 在 shell 启动时查找.zshrc文件,因此要在获取 bash 配置文件内容的同时继续使用 zsh,您可以运行以下命令:

echo "source ~/.bash_profile" >> ~/.zshrc

Open a new terminal window for the changes to take effect.打开一个新的终端窗口以使更改生效。

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,但它确实有效。

source ~/.profile添加到我的.bash_profile对我.bash_profile

As of High Sierra, both Terminal and iTerm want to load ~/.profile first.从 High Sierra 开始,Terminal 和 iTerm 都希望首先加载 ~/.profile。 So I suggest you put one line in your .profile to make your Mac work like other Unixes:所以我建议你在你的 .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 的怪异行为。

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。 iTerm runs a login shell by default. iTerm 默认运行登录 shell。 Verify in iTerm Preferences > General > Command > (*) Login Shell Therefore, I know that ~/.bash_profile will always be called.在 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.另请注意,首先加载 /etc/profile,但由于我从未使用过该系统范围的 init 文件,所以我知道这不是我的问题。 For details check out $ man bash有关详细信息,请查看$ man bash

So, I started with my ~/.bash_profile file.所以,我从我的 ~/.bash_profile 文件开始。

I found that when I installed Canopy Express that 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'

ps 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

As of Catalina the default shell is now zsh.从 Catalina 开始,默认 shell 现在是 zsh。 You can change it back to bash with chsh -s /bin/bash and that should load your .profile or .bash_profile您可以使用chsh -s /bin/bash将其改回chsh -s /bin/bash并且应该加载您的.profile.bash_profile

I am guessing you may use another shell, such as bash, tcsh, sh, zsh etc.我猜你可能会使用另一个 shell,比如 bash、tcsh、sh、zsh 等。

Put source .bash_profile into your appropriate 'bashrc' file will make the auto loading recovered, iesource .bash_profile放入适当的“bashrc”文件中将使自动加载恢复,即

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

Most likely, you need to create the files yourself as they appear not to exist by default.最有可能的是,您需要自己创建这些文件,因为它们在默认情况下似乎不存在。 You should give them execute permission to make them run.你应该给他们执行权限让他们运行。

~ % sudo chmod 700 ~/.bash_profile

Also, you should check the ownership of the files.此外,您应该检查文件的所有权。 They should belong to current user rather than root.它们应该属于当前用户而不是 root。 Otherwise, you will get permission denied error.否则,您将收到permission denied错误。

~ % ls -a -l

~ % sudo chown <user_name> ~/.bash_profile

Finally, please note that bash looks in your home directory for .bash_profile, .bash_login, and .profile in order.最后,请注意 bash 按顺序在您的主目录中查找 .bash_profile、.bash_login 和 .profile。 Bash will stop looking if the first is found.如果找到第一个,Bash 将停止查找。 This means if you have both .bash_profile and .profile files, the .profile will not run.这意味着如果您同时拥有 .bash_profile 和 .profile 文件,则 .profile 将不会运行。 For more information 想要查询更多的信息

Hope this would help you.希望这会帮助你。

My issue was solved by unchecking Preferences > General > tmux >我的问题是通过取消选中 Preferences > General > tmux > 解决的

Use "tmux" profile rather than profile of the connecting session使用“tmux”配置文件而不是连接会话的配置文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM