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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 17:00:21  来源:igfitidea点击:

Why must I source .bashrc every time I open terminal for aliases to work?

bashshell

提问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 . ~/.bashrcevery 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 sourceof the .bashrcfile in this file but it is a read-only file. I am not able to add the sourceof the .bashrcfile 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 bashas a login shell.

看起来您的终端模拟器正在bash作为登录 shell启动。

If that's the case, it will read /etc/profilefor 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 .bashrcfile. 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。当您想从命令行运行函数时,这是必不可少的。

  1. Start up Terminal
  2. Type cd ~/to go to your home folder
  3. Type touch .bash_profileto create your new file.
  4. Edit .bash_profile with your favorite editor (or you can just type open -e .bash_profileto open it in TextEdit.
  5. [ -f "$HOME/.bashrc" ] && source "$HOME/.bashrc"Save it and close it
  1. 启动终端
  2. 键入cd ~/以转到您的主文件夹
  3. 键入touch .bash_profile以创建新文件。
  4. 使用您最喜欢的编辑器编辑 .bash_profile(或者您可以直接open -e .bash_profile在 TextEdit 中输入以打开它。
  5. [ -f "$HOME/.bashrc" ] && source "$HOME/.bashrc"保存并关闭它

Restart the terminal, It should work

重新启动终端,它应该可以工作