bash shell的配置文件很多,可以分成下面类别
全局配置:针对所有用户皆有效
/etc/profile
/etc/profile.d/*.sh
/etc/bashrc
个人配置:只针对特定用户有效
~/.bash_profile
~/.bashrc
直接通过终端输入账号密码登录
使用su - UserName切换的用户
配置文件生效和执行顺序:
# 放在每个文件最前
/etc/profile
/etc/profile.d/*. sh
/etc/bashrc
~/ .bash_profile
~/ .bashrc
/etc/bashrc
# 放在每个文件最后
/etc/profile.d/*.sh
/etc/bashrc
/etc/profile
/etc/bashrc # 此文件执行两次
~/.bashrc
~/.bash_profile
注意:文件之间的调用关系,写在同一个文件的不同位置,将影响文件的执行顺序
su UserName
图形界面下打开的终端
执行脚本
任何其它的bash实例
执行顺序
/etc/profile.d/*.sh
/etc/bashrc
~/.bashrc
profile类和bashrc类
profile类为交互式登录的shell提供配置
全局:/etc/profile, /etc/profile.d/*.sh
个人:~/.bash_profile
使用场景:
用于定义环境变量
运行命令或脚本
bashrc类:为非交互式和交互式登录的shell提供配置
全局:/etc/bashrc
个人:~/.bashrc
使用场景
修改profile和bashrc文件后需生效两种方法:
重新启动shell进程
source | . 配置文件
注意:source 会在当前shell中执行脚本,所有一般只用于执行置文件,或在脚本中调用另一个脚本的场景
示例
. ~/.bashrc
保存在~/.bash_logout文件中(用户),在退出登录shell时运行
功能:
创建自动备份
清除临时文件
原文:https://www.cnblogs.com/wysxr/p/14629126.html