温欣爸比

  • 主页
  • Alfred Workflow
  • 《Vim 练级手册》
  • 常用命令
  • 代码笔记
  • 合辑
  • 在线工具
所有文章 友链 关于我

温欣爸比

  • 主页
  • Alfred Workflow
  • 《Vim 练级手册》
  • 常用命令
  • 代码笔记
  • 合辑
  • 在线工具

Shell 环境中 bash_profile 和 bashrc 的区别

2018-06-01

在 shell 环境中 ~/.bash_profile 和 ~/.bashrc 文件都可以都可以配置环境变量等信息,那到底该写到那个文件里边呢?



  • /etc/profile
  • ~/.bash_profile ~/.bash_login ~/.profile
  • ~/.bashrc
  • /etc/bashrc
  • ~/.bash_logout


首先来了解下 Linux 在登录时会按照如下顺序执行配置文件

1
/etc/profile -> (~/.bash_profile | ~/.bash_login | ~/.profile) -> ~/.bashrc -> /etc/bashrc -> ~/.bash_logout

/etc/profile

该文件为系统所有用户设置环境信息,只在用户登录时执行一次,并且执行 /etc/profile.d 目录下的配置信息

~/.bash_profile ~/.bash_login ~/.profile

当前登录用户的环境信息,只在登录时执行一次。

三个文件只会按照顺序执行一个,并且都会执行 ~/.bashrc 文件,因为在 ~/.bash_profile 有如下语句

1
2
3
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi

其中 CentOS 会用 ~/.bash_profile 文件,Ubuntu 用 ~/.profile 文件

~/.bashrc

当前登录用户的 shell 配置,在执行 ~/.bash_profile 后会执行该文件

它是交互式 non-login 方式,所以每次打开新 shell 都会执行

/etc/bashrc

如果 ~/.bashrc 文件中有

1
2
3
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi

则也会执行它,同时会执行 /etc/profile.d 目录下的配置

~/.bash_logout

最后用户登出时还会执行 ~/.bash_logout

最后总结下如果是配置环境变量等信息推荐写到 ~/.bash_profile 文件中,shell 信息比如 alias 等配置推荐写到 ~/.bashrc 中

最近更新
Alfred Workflow 命令行帮助工具
最近热读
Go 判断数组中是否包含某个 item
Vim 高级功能 vimgrep 全局搜索文件
办理北京工作居住证的一些细节
Go 语法错误:Non-declaration statement outside function body
Mac 电脑查看字体文件位置
扫码关注公众号,或搜索公众号“温欣爸比” 及时获取我的最新文章
赏

谢谢你请我喝咖啡

支付宝
微信
  • shell
Linux 输出内容到文件权限不够
Expect file 相关操作
  1. 1. /etc/profile
  2. 2. ~/.bash_profile ~/.bash_login ~/.profile
  3. 3. ~/.bashrc
  4. 4. /etc/bashrc
  5. 5. ~/.bash_logout
© 2017 - 2022 温欣爸比 京ICP备15062634号 总访问量3506次 访客数3458人次 本文总阅读量1次
Hexo Theme Yilia by Litten
  • 所有文章
  • 友链
  • 关于我

tag:

  • python
  • flask
  • javascript
  • docker
  • 工具
  • openresty
  • 微信
  • java
  • hexo
  • 杂谈
  • vim
  • git
  • mysql
  • http
  • linux
  • mac
  • tmux
  • ssh
  • 算法
  • 开发
  • node
  • 杂文
  • jinja2
  • maven
  • spring
  • 北京
  • 生活
  • springboot
  • react
  • shell
  • graphql
  • iterm
  • expect
  • nginx
  • sqlalchemy
  • html
  • electron
  • vagrant
  • elastic
  • 宝贝
  • ansible
  • css
  • jquery
  • go
  • markdown
  • awk
  • redis
  • leetcode
  • zsh
  • 漫威
  • ssr
  • android
  • ffmpeg
  • chrome
  • vmware
  • youtube
  • windows
  • jupyter
  • excel
  • jq
  • Mac
  • Homebrew
  • mongo
  • py2
  • HomeBrew
  • movie
  • nodejs

    缺失模块。
    1、请确保node版本大于6.2
    2、在博客根目录(注意不是yilia根目录)执行以下命令:
    npm i hexo-generator-json-content --save

    3、在根目录_config.yml里添加配置:

      jsonContent:
        meta: false
        pages: false
        posts:
          title: true
          date: true
          path: true
          text: false
          raw: false
          content: false
          slug: false
          updated: false
          comments: false
          link: false
          permalink: false
          excerpt: false
          categories: false
          tags: true
    

  • Guru99
每天看书
每天背单词
每天一篇
写写代码
听听周杰伦
爱爱老婆