从 zsh 中输入 bash 时如何加载 ~/.bash_profile?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23233603/
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
How to load ~/.bash_profile when entering bash from within zsh?
提问by Blaszard
I've used bash for two years, and just tried to switch to zsh shell on my OS X via homebrew. And I set my default (login) shell to zsh, and I confirmed it's set properly by seeing that when I launch my Terminal, it's zsh shell that is used in default.
我已经使用 bash 两年了,只是尝试通过自制软件在我的 OS X 上切换到 zsh shell。我将我的默认(登录)shell 设置为 zsh,我确认它设置正确,当我启动我的终端时,它是默认使用的 zsh shell。
However, when I try to enter bash shell from within zsh, it looks like not loading ~/.bash_profile
, since I cannot run my command using aliases, which is defined in my ~/.bash_profile
like alias julia="~/juila/julia"
, etc.. Also, the prompt is not what I set in the file and instead return bash-3.2$
.
但是,当我尝试从 zsh 中输入 bash shell 时,它看起来像是没有加载~/.bash_profile
,因为我无法使用别名运行我的命令,别名在我的~/.bash_profile
likealias julia="~/juila/julia"
等中定义。此外,提示不是我在文件中设置的而是返回bash-3.2$
。
For some reasons, when I set my login shell to bash, and enter zsh from within bash, then ~/.zshrc
is loaded properly.
由于某些原因,当我将登录 shell 设置为 bash 并从 bash 中输入 zsh 时,~/.zshrc
会正确加载。
So why is it not loaded whenever I run bash
from within zsh? My ~/.bash_profile
is symbolic linked to ~/Dropbox/.bash_profile
in order to sync it with my other computers. Maybe does it cause the issue?
那么为什么当我bash
从 zsh 中运行时它没有加载?我~/.bash_profile
是符号链接~/Dropbox/.bash_profile
,以便与我的其他计算机同步。也许它会导致问题?
采纳答案by Keith Thompson
An interactive bash
reads your ~/.bash_profile
if it's a login shell, or your ~/.bashrc
if it's not a login shell.
如果它是登录外壳,交互bash
会读取您的信息,~/.bash_profile
如果它不是登录外壳,则会读取您的信息~/.bashrc
。
A typical .bash_profile
will contain something like:
典型的.bash_profile
将包含以下内容:
if [ -f ~/.bashrc ]; then . ~/.bashrc; fi
so .bashrc
can contain commands to be executed by either login or non-login shells.
so.bashrc
可以包含要由登录或非登录 shell 执行的命令。
If you run bash -l
rather than just bash
, it should read your .bash_profile
.
如果您运行bash -l
而不仅仅是运行bash
,它应该读取您的.bash_profile
.
Reference: https://www.gnu.org/software/bash/manual/html_node/Bash-Startup-Files.html
参考:https: //www.gnu.org/software/bash/manual/html_node/Bash-Startup-Files.html
回答by Yuchen Zhong
Open ~/.zshrc
, and at the very bottom of the file, add the following:
打开~/.zshrc
,并在文件的最底部,添加以下内容:
if [ -f ~/.bash_profile ]; then
. ~/.bash_profile;
fi
Every time you open the terminal, it will load whatever is defined in ~/.bash_profile
(if the file exist). With that, you can keep your custom settings for zsh(colors, and etc). And you get to keep your custom shell settings in .bash_profile
file.
每次打开终端时,它都会加载其中定义的任何内容~/.bash_profile
(如果文件存在)。这样,您可以保留zsh的自定义设置(颜色等)。您可以将自定义 shell 设置保存在.bash_profile
文件中。
This is much cleaner than using bash -l
IMO.
这比使用bash -l
IMO干净得多。
If you prefer putting your settings in .bashrc
, or .bash_login
, or .profile
, you can do the same for them.
如果您更喜欢将设置放在.bashrc
、 或.bash_login
、 或 中.profile
,您可以对它们执行相同的操作。
回答by mklement0
To complement @Keith Thompson's excellent answer:
为了补充@Keith Thompson 的出色回答:
macOS:
系统:
As @chepner puts it succinctly(emphasis mine):
正如@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.
在 OS X 中,bash 不用作初始 [在启动时] 登录过程的一部分,并且 Terminal.app(或其他终端模拟器)进程存在于任何预先存在的 bash 会话之外,因此每个新窗口 [或选项卡 -阅读:交互式 bash shell](默认情况下)将自己视为新的登录会话。
As a result, some OSX users only ever create ~/.bash_profile
, and never bother with ~/.bashrc
, because ALL interactive bash shells are loginshells.
结果,一些 OSX 用户只创建~/.bash_profile
,从不打扰~/.bashrc
,因为所有交互式 bash shell 都是登录shell。
Linux:
Linux:
On Linux, the situation is typically reversed:
bash
shells created interactively are [interactive] NON-loginshells, so it is ~/.bashrc
that matters.
在 Linux 上,情况通常是相反的:
bash
交互创建的 shell 是 [交互式]非登录shell,所以这很~/.bashrc
重要。
As a result, many Linux users only ever deal with ~/.bashrc
.
因此,许多 Linux 用户只处理~/.bashrc
.
To maintain bash profiles that work on BOTH platforms, use the technique @Keith Thompson mentions:
要维护适用于两个平台的 bash 配置文件,请使用@Keith Thompson 提到的技术:
- Put your definitions (aliases, functions, ...) in
~/.bashrc
- Add the following line to
~/.bash_profile
- 将您的定义(别名、函数等)放入
~/.bashrc
- 将以下行添加到
~/.bash_profile
[[ -f ~/.bashrc ]] && . ~/.bashrc
回答by Mussa Charles
For those who have just installed zsh and want their alias from bash to work on zsh do the following
对于那些刚刚安装 zsh 并希望他们的 bash 别名在 zsh 上工作的人,请执行以下操作
Open .zshrc file in vim like so
vi ~/.zshrc
Scroll to the bottom
- click "i" to enable write mode
- Tell zsh to load items from bash_profile when needed like so
source ~/.bash_profile
- Write and quit like so
:wq
- Refresh your zsh like so
That's it. Now all your saved alias in .bash_profile will be ready to use in zsh.source ~/.zshrc
像这样在vim中打开.zshrc文件
vi ~/.zshrc
滚动到底部
- 单击“i”启用写入模式
- 告诉 zsh 在需要时从 bash_profile 加载项目,就像这样
source ~/.bash_profile
- 像这样写然后退出
:wq
- 像这样刷新你的 zsh
就是这样。现在,您在 .bash_profile 中保存的所有别名都可以在 zsh 中使用了。source ~/.zshrc
回答by Sandeep Kumar
Copy the contents from ~/.bash_profile and paste them at the bottom of ~/.zshrc file.
从 ~/.bash_profile 复制内容并将它们粘贴到 ~/.zshrc 文件的底部。
回答by Yahel
For ZSH users on MacOs, I ended up with a one liner.
对于 MacOs 上的 ZSH 用户,我最终得到了一个衬垫。
At the very bottom of the ~/.zshrcI added the following line :
在~/.zshrc 的最底部,我添加了以下行:
bash -l
What it does is simply load the .bash_profilesettings(aliases, function, export $PATH, ...)
它所做的只是加载.bash_profile设置(别名、函数、导出 $PATH、...)
If you decide to get rid of ZSH and go back to plain BASH, you'll be back to normal with no hassle at all.
如果您决定摆脱 ZSH 并返回到普通的 BASH,您将完全恢复正常。
回答by theNbomr
If this is something that you do infrequently, or it just isn't appropriate to make changes, you can also 'source' the .bash_profileafter launching the child bash shell.
如果这是您很少做的事情,或者不适合进行更改,您还可以在启动子 bash shell 后“获取” .bash_profile。
. ~/.bash_profile
This will pull in the settings you make in the .bash_profilescript for the life of that shell session. In most cases, you should be able to repeat that command, so it's also an easy way to test any changes that you make without needing to do a full login, as well as bring all of your existing shell sessions up-to-date if you make upgrades to the .bash_profile&/or .bashrcfiles.
这将在该shell 会话的生命周期中引入您在.bash_profile脚本中所做的设置。在大多数情况下,您应该能够重复该命令,因此这也是一种无需完全登录即可测试您所做的任何更改的简单方法,并且如果出现以下情况,则可以使所有现有的 shell 会话保持最新状态您对.bash_profile和/或.bashrc文件进行升级。
回答by haccks
回答by nabster
I am using a zsh framework called oh my zshand I have tried most of the solutions listed here and it broke the format for my custom theme. However, these steps worked for me.
我正在使用一个名为oh my zsh的 zsh 框架,我已经尝试了此处列出的大多数解决方案,但它破坏了我的自定义主题的格式。但是,这些步骤对我有用。
Add new alias(es) at the bottom of my .bash_profile
vi ~/.bash_profile
Make zshto load items from .bash_profile
source ~/.bash_profile
Refresh zsh
source ~/.zshrc
Restart OSX Terminal app
Try your new alias!
在我的.bash_profile底部添加新别名
vi ~/.bash_profile
让zsh从.bash_profile加载项目
source ~/.bash_profile
刷新zsh
source ~/.zshrc
重新启动 OSX 终端应用程序
试试你的新别名!