引入:
Linux下一切皆文件,对于bash的配置也是通过文件来配置的。通过配置bash可以达到bash更加容易使用的目的。
目录
1. bash配置文件的分类
按照生效范围划分,存在两类:
全局配置文件:对于所有用户有效。
/etc/profile
/etc/profile.d/*.sh
个人配置文件:仅对各个用户有效。
~/.bashrc
~/.bash_profile
按照功能分类,存在两类:
profile类:为交互式登陆的shell提供配置:
全局:/etc/profile,/etc/profile.d/*.sh
个人:~/.bash_profile
作用:
1、用于定义环境变量
2、在登录交互式的shell时运行命令或脚本
bashrc类:为非交互式登录的shell提供配置
全局:/etc/bashrc
个人:~/.bashrc
功用:
1、定义命令别名
2、定义本地变量
2.shell登录类型
- 交互式:
直接通过终端输入账号和密码,使用su - USERNAME登录。
交互式的读取顺序:/etc/profile-->/etc/profile.d/*.sh-->~.bash_profile-->~/.bashrc-->/etc/bashrc - 非交互式:
使用su USERNAME或图形界面下打开的终端,执行脚本。
非交互式的读取顺序~.bashrc-->/etc/bashrc-->/etc/profile.d/*.sh
通过配置文件修改的配置生效方式:
1、退出并重新登录
2、让bash重读此配置文件,有以下两种方式:
. FILE
source FILE
3.使用举例
# 在登录时,告诉用户现在的时间。
[root@localhost ~]# vim /etc/profile.d/welcome.sh
[root@localhost ~]# cat /etc/profile.d/welcome.sh
echo "welcome to my Linux"
date
[root@localhost ~]# source /etc/profile.d/welcome.sh
welcome to my Linux
Sat Mar 9 13:42:59 CST 2019
# 创建命令别名catn,使其等于cat -n,并使其对所有用户永久生效。
[root@localhost ~]# vim /etc/bashrc
[root@localhost ~]# tail /etc/bashrc -n1
alias catn="cat -n"
[root@localhost ~]# source /etc/bashrc
[root@localhost ~]# catn /etc/fstab
1
2 #
3 # /etc/fstab
4 # Created by anaconda on Sun Oct 15 02:13:15 2017
5 # this is a pollution message
6 # Accessible filesystems, by reference, are maintained under '/dev/disk'
7 # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
8 # this is a pollution message
9 /dev/mapper/centos-root / xfs defaults 0 0
10 UUID=3cc31987-a4dc-4ecb-8cbd-9a2db1f8a2ec /boot xfs defaults 0 0
11 /dev/mapper/centos-swap swap swap defaults 0 0
12 /dev/cdrom /media/cdrom iso9660 defaults 0 0
# 定义环境变量BAIDU:
[root@localhost ~]# vim /etc/profile
[root@localhost ~]# tail -n1 /etc/profile
export BAIDU="www.baidu.com"
[root@localhost ~]# . /etc/profile
[root@localhost ~]# echo $BAIDU
www.baidu.com
# 定义本地变量google
[root@localhost ~]# vim /etc/bashrc
[root@localhost ~]# tail -n1 /etc/bashrc
google="www.google.com"
[root@localhost ~]# . /etc/bashrc
[root@localhost ~]# echo $google
www.google.com