Linux bashrc和profile的用途和区别

管理员 Linux1 30,2064字数 1108阅读3分41秒阅读模式

使用终端ssh登录Linux操作系统的控制台后,会出现一个提示符号(例如:#或~),在这个提示符号之后可以输入命令,Linux根据输入的命令会做回应,这一连串的动作是由一个所谓的Shell来做处理。

Shell是一个程序,最常用的就是Bash,这也是登录系统默认会使用的Shell。文章源自运维生存时间-https://www.ttlsa.com/linux/linux-bashrc-and-profile/

bashrc和profile都是Shell的启动设置文件(其实这两个文件也是Shell脚本),可以为当前的Shell初始化环境变量等,它们类似Microsoft DOS/Windows下的autoexec.bat文件。文章源自运维生存时间-https://www.ttlsa.com/linux/linux-bashrc-and-profile/

下面详细介绍bashrc和profile的用途和区别:文章源自运维生存时间-https://www.ttlsa.com/linux/linux-bashrc-and-profile/

针对个别用户

用户HOME(家)目录/.bashrc文章源自运维生存时间-https://www.ttlsa.com/linux/linux-bashrc-and-profile/

# ~/.bashrc: executed by bash(1) for non-login shells.

用户HOME(家)目录/.profile文章源自运维生存时间-https://www.ttlsa.com/linux/linux-bashrc-and-profile/

# ~/.profile: executed by Bourne-compatible login shells.

针对全体用户

/etc/bash.bashrc
# System-wide .bashrc file for interactive bash(1) shells.
/etc/profile
# /etc/profile: system-wide .profile file for the Bourne shell (sh(1)

bashrc和profile的差异

从上面的英文描述可以知道,bashrc和profile的差异在于:文章源自运维生存时间-https://www.ttlsa.com/linux/linux-bashrc-and-profile/

1. bashrc是在系统启动后就会自动运行。文章源自运维生存时间-https://www.ttlsa.com/linux/linux-bashrc-and-profile/

2. profile是在用户登录后才会运行。文章源自运维生存时间-https://www.ttlsa.com/linux/linux-bashrc-and-profile/

3. 进行设置后,可运用source bashrc命令更新bashrc,也可运用source profile命令更新profile。文章源自运维生存时间-https://www.ttlsa.com/linux/linux-bashrc-and-profile/

PS:通常我们修改bashrc,有些linux的发行版本不一定有profile这个文件文章源自运维生存时间-https://www.ttlsa.com/linux/linux-bashrc-and-profile/

4. /etc/profile中设定的变量(全局)的可以作用于任何用户,而~/.bashrc等中设定的变量(局部)只能继承/etc/profile中的变量,他们是"父子"关系。文章源自运维生存时间-https://www.ttlsa.com/linux/linux-bashrc-and-profile/

补充介绍

另外,需要补充说明介绍bashrc相关的几个文件:文章源自运维生存时间-https://www.ttlsa.com/linux/linux-bashrc-and-profile/

~/.bash_profile: 每个用户都可使用该文件输入专用于自己使用的shell信息,当用户登录时,该文件仅仅执行一次!默认情况下,他设置一些环境变量,执行用户的.bashrc文件。文章源自运维生存时间-https://www.ttlsa.com/linux/linux-bashrc-and-profile/

~/.bash_logout: 当每次退出系统(退出bash shell)时,执行该文件。文章源自运维生存时间-https://www.ttlsa.com/linux/linux-bashrc-and-profile/

~/.bash_profile 是交互式、login方式进入bash运行的,~/.bashrc是交互式non-login方式进入bash运行的,通常二者设置大致相同,所以通常前者会调用后者。文章源自运维生存时间-https://www.ttlsa.com/linux/linux-bashrc-and-profile/ 文章源自运维生存时间-https://www.ttlsa.com/linux/linux-bashrc-and-profile/

weinxin
我的微信
微信公众号
扫一扫关注运维生存时间公众号,获取最新技术文章~
管理员
  • 本文由 发表于 30/11/2015 01:00:46
  • 转载请务必保留本文链接:https://www.ttlsa.com/linux/linux-bashrc-and-profile/
  • bash
  • bashrc
  • linux bash
  • profile
  • shell
评论  1  访客  1
    • guest
      guest 9

      bashrc是在系统启动后就会自动运行
      profile是在用户登录后才会运行。

    评论已关闭!