Kevin_306

06-SHELL脚本编程基础-bash的配置文件20210404 (七)

bash shell的配置文件很多,按功能划分分类

1、profile类和bashrc类

   1.1Profile类:

    profile类为交互式登录的shell提供配置 

全局:/etc/profile, /etc/profile.d/*.sh 
个人:~/.bash_profile

  主要功用:
  (1) 用于定义环境变量

  (2) 运行命令或脚本

 

   1.2Bashrc类:

    bashrc类:为非交互式和交互式登录的shell提供配置

全局:/etc/bashrc 
个人:~/.bashrc

  主要功用:
   (1) 定义命令别名和函数
   (2) 定义本地变量 

2、编辑配置文件生效

修改profile和bashrc文件后需生效两种方法: 

1. 重新启动 shell进程 
2. source|.   配置文件

范例:

 .    ~/.bashrc

3、Bash 退出任务

保存在~/.bash_logout文件中(用户),在退出登录shell时运行

功能:

1.创建自动备份 
2.清除临时文件

还有一个文件.bash_logout,这个基本默认是空的,

编辑这个文件,比如加入rm -rf /data/*,每次退出就会自动清空/data/里面的文件

  然后退出exit(第一次退出的是bash shell的子shell环境),再次真正退出exit

  登陆后查看,/data/下面的文件确实没有了

别名alias建议放在 .bashrc里面

环境变量建议放在 .bash_profile里面  

举例说明:

[root@centos7-kevin306 ~]# vim .bash_profile (自定义一个别名abc)
# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
    . ~/.bashrc
fi

# User specific environment and startup programs

PATH=$PATH:$HOME/bin

export PATH
alias abc=hostname                                                                                        
~                                                                                                         
~                                                                                                         
~                                                                                                         
~                                                                                                         
~                                                                                                         
~                                                                                                         
~                                                                                                         
~                                                                                                         
~                                                                                                         
~                                                                                                         
~                                                                                                         
~                                                                                                         
~                                                                                                         
".bash_profile" 13L, 195C written                                                       
[root@centos7-kevin306 ~]# . .bash_profile (. 生效配置)
[root@centos7-kevin306 ~]# alias abc
alias abc='hostname'

Last login: Mon Apr 5 02:31:44 2021 from 192.168.0.101
.bashrc  (退出X-shell之后,重新登陆后的执行顺序)
.bash_profile
[root@centos7-kevin306 ~]# bash (#开了字shell,只执行.bashrc)
.bashrc

 

posted on 2021-04-04 23:11  熊猫小虾  阅读(87)  评论(0编辑  收藏  举报

导航