How to load ~/.bash_profile when entering bash from within zsh?
我已经使用bash两年了,只是尝试通过自制程序在OS X上切换到zsh shell。 然后将默认(登录)shell设置为zsh,并通过查看启动终端时使用的默认zsh shell来确认设置正确。
但是,当我尝试从zsh中进入bash shell时,好像没有加载
由于某些原因,当我将登录shell设置为bash并从bash中输入zsh时,将正确加载
那么,为什么每当我在zsh中运行
打开
1 2 3 | if [ -f ~/.bash_profile ]; then . ~/.bash_profile; fi |
每次打开终端时,它将加载
这比使用
如果您希望将设置放入
如果交互式
典型的
1 | if [ -f ~/.bashrc ]; then . ~/.bashrc; fi |
因此
如果您运行
参考:https://www.gnu.org/software/bash/manual/html_node/Bash-Startup-Files.html
为了补充@Keith Thompson的出色答案:
苹果系统:
正如@chepner简洁地说(强调我的):
In OS X, bash is not used as part of the initial [at boot time] login process, and the Terminal.app (or other terminal emulators) process exists outside any pre-existing bash sessions, so each new window [or tab - read: interactive bash shell] (by default) treats itself as a new login session.
结果,某些OSX用户只能创建
Linux:
在Linux上,情况通常是相反的:
交互式创建的
结果,许多Linux用户只使用
要维护在两个平台上均可使用的bash配置文件,请使用@Keith Thompson提到的技术:
-
将您的定义(别名,函数等)放在
~/.bashrc 中 -
将以下行添加到
~/.bash_profile
1 | [[ -f ~/.bashrc ]] && . ~/.bashrc |
从?/ .bash_profile复制内容,并将其粘贴到?/ .zshrc文件的底部。
对于刚刚安装zsh并希望其bash别名在zsh上运行的用户,请执行以下操作
像这样在vim中打开.zshrc文件
1 | vi ~/.zshrc |
滚动到底部
1 | source ~/.bash_profile |
1 | :wq |
1 | source ~/.zshrc |
而已。现在,您在.bash_profile中保存的所有别名都可以在zsh中使用。
对于MacOs上的ZSH用户,我最终得到了一线。
在?/ .zshrc的最底部,我添加了以下行:
1 | bash -l |
它的作用就是简单地加载.bash_profile设置(别名,函数,导出$ PATH等)。
如果您决定摆脱ZSH并返回普通BASH,则将毫无麻烦地恢复正常。
如果您不经常这样做,或者不适合进行更改,则还可以在启动子bash shell之后"获取" .bash_profile。
1 | . ~/.bash_profile |
这将提取您在.bash_profile脚本中所做的设置,以用于该Shell会话的整个生命周期。在大多数情况下,您应该能够重复该命令,因此这也是测试您所做的任何更改的简便方法,而无需进行完整的登录,并且可以使所有现有的Shell会话保持最新您可以升级到.bash_profile和/或.bashrc文件。
最近,我在OS X上安装了oh-my-zsh并将
我通过在