Linux下/etc/profile、/etc/bashrc、~/.profile、~/.bashrc的区别

Linux系统下/etc/profile、/etc/bashrc、~/.profile、~/.bashrc的区别

/ect/目录:作用在整个系统

在这里插入图片描述

~/目录:作用在当前用户:

在这里插入图片描述

四者之间的差异

  • /etc/profile:

用来设置系统环境参数:比如$PATH. 环境变量是对系统内所有用户生效。

  • ~/.bash_profile 或 ~/.profile:

用来设置用户环境变量:类似/etc/profile,但是针对用户来设定的,这个环境变量只对当前用户生效.

  • /etc/bashrc 或者 /etc/bash.bashrc:

初始化系统bash shell:每一个运行bash shell的用户执行此文件,比如初始化 bash 的设置, bash 的代码补全, bash 的别名, bash 的颜色. 当bash shell被打开时,该文件被读取。当用户shell执行了bash时,运行这个文件。bashrc在Ubuntu下叫做bash.bashrc

  • ~/.bashrc:

初始化当前用户 bash shell:类似于/etc/bashrc, 作用范围是当前用户,不对其他用户生效。

  • 注意:

/etc/profile中设定的变量(全局)的可以作用于任何用户,而~/.bashrc等中设定的变量(局部)只能继承/etc/profile中的变量,他们是"父子"关系.
~/.bash_profile 是交互式、login 方式进入 bash 运行的,只有用户登录时才会生效。
~/.bashrc 是交互式 non-login 方式进入 bash 运行的,用户不一定登录,只要以该用户身份运行命令行就会读取该文件。

  • 执行顺序

在刚登录Linux时,首先启动 /etc/profile 文件,然后再启动用户目录下的 ~/.bash_profile、 ~/.bash_login或 /.profile文件中的其中一个,执行的顺序为:/.bash_profile、 ~/.bash_login、 ~/.profile。如果 ~/.bash_profile文件存在的话,一般还会执行 ~/.bashrc文件。

猜你喜欢

转载自blog.csdn.net/weixin_44307065/article/details/108895008