bash 为什么每次打开终端时都必须输入 .bashrc 以使别名工作?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/51876792/
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
Why must I source .bashrc every time I open terminal for aliases to work?
提问by Mayank Patel
Git was working fine. I have created an alias in Git but the issue is when I tried to reopen the terminal, then I need to run . ~/.bashrc
every time in the terminal.
Git 工作正常。我在 Git 中创建了一个别名,但问题是当我尝试重新打开终端时,我. ~/.bashrc
每次都需要在终端中运行。
What is the best way I don't need to provide source every time when I reopen the terminal?
每次重新打开终端时不需要提供源的最佳方法是什么?
What I did?I am trying to add source
of the .bashrc
file in this file but it is a read-only file. I am not able to add the source
of the .bashrc
file in this profile.
我做了什么?我想补充source
的的.bashrc
这个文件中的文件,但它是一个只读文件。我不能添加source
的的.bashrc
文件,在此配置文件。
open /etc/profile
Added the permission to write in the profile as well, still not able to link the source file.
还添加了在配置文件中写入的权限,但仍然无法链接源文件。
sudo chmod u+w /etc/profile
Profile:
轮廓:
# System-wide .profile for sh(1)
if [ -x /usr/libexec/path_helper ]; then
eval `/usr/libexec/path_helper -s`
fi
if [ "${BASH-no}" != "no" ]; then
[ -r /etc/bashrc ] && . /etc/bashrc
fi
回答by Aserre
It looks like your terminal emulator is launching bash
as a login shell.
看起来您的终端模拟器正在bash
作为登录 shell启动。
If that's the case, it will read /etc/profile
for configuration as well as 1 of the following files, if they exist (listed in order of importance) :
如果是这种情况,它将读取/etc/profile
配置以及以下文件之一,如果它们存在(按重要性顺序列出):
- ~/.bash_profile
- ~/.bash_login
- ~/.profile
- ~/.bash_profile
- ~/.bash_login
- ~/.profile
It will thus ignore your .bashrc
file. A correct fix for your situation would be to either configure your terminal emulator to run bash interactively and non-login, or add [ -f "$HOME/.bashrc" ] && source "$HOME/.bashrc"
to your ~/.bash_profile
因此它将忽略您的.bashrc
文件。针对您的情况的正确解决方法是将您的终端模拟器配置为以交互方式和非登录方式运行 bash,或者添加[ -f "$HOME/.bashrc" ] && source "$HOME/.bashrc"
到您的~/.bash_profile
Hereis a link to the documentation about which files are loaded depending of the type of shell you are running
这是有关根据您正在运行的 shell 类型加载哪些文件的文档的链接
回答by Mayank Patel
As per @Aserre's answer i have followed this step to solve this issue
根据@Aserre 的回答,我已按照此步骤解决此问题
A typical install of OS won't create a .bash_profile for you. When you want to run functions from your command line, this is a must-have.
操作系统的典型安装不会为您创建 .bash_profile。当您想从命令行运行函数时,这是必不可少的。
- Start up Terminal
- Type
cd ~/
to go to your home folder - Type
touch .bash_profile
to create your new file. - Edit .bash_profile with your favorite editor (or you can just type
open -e .bash_profile
to open it in TextEdit. [ -f "$HOME/.bashrc" ] && source "$HOME/.bashrc"
Save it and close it
- 启动终端
- 键入
cd ~/
以转到您的主文件夹 - 键入
touch .bash_profile
以创建新文件。 - 使用您最喜欢的编辑器编辑 .bash_profile(或者您可以直接
open -e .bash_profile
在 TextEdit 中输入以打开它。 [ -f "$HOME/.bashrc" ] && source "$HOME/.bashrc"
保存并关闭它
Restart the terminal, It should work
重新启动终端,它应该可以工作