现在的位置: 首页 > 综合 > 正文

redhat bash 初始化设置

2013年07月19日 ⁄ 综合 ⁄ 共 2753字 ⁄ 字号 评论关闭

先说明三个概念

 

登录shell

正常登录程序启动的shell.既登录成功后紧接着给登录用户启动的shell.

 

非登录交互式shell

这个shell的工作方式是交互式的,等用户输入,然后执行,再等用户输入。显然登录shell就是一个交互式shell。

如下,我们可获得一个交互式非登录shell:

[root@localhost ~]# bash
[root@localhost ~]# pwd
/root

 

非交互式shell

为运行一个shell脚本启动的shell.

 

以FC5的bash为例,跟shell环境配置相关的文件以下几个,

 

/etc/profile
/etc/profile.d/*.sh
/etc/bashrc
~/.bash_profile
~/.bashrc

 

有时你会发现定义一个别名,有时好像在任意一个文件里定义都可以起作用,有时好像又不起作用,那是为什么呢?这些配置文件各自互责了什么工作?相互的关系是怎么样的?跟前面介绍的不同种类的shell的关系是如何的呢?下面对每个文件单独进行说明。

 

/etc/profile

Linux规定,当启动一个登录shell会执行这个脚本. 测试过程如下:

把LIST的定义加到/etc/profile文件的未尾并保存. 如下:
alias LIST='ls -l'

把所有其它shell配置文件或目录改名,这样系统就找不到那些shell脚本了,不会执行,重而避免其它配置文件的干扰。如下:
[root@localhost ~]# mkdir /etc/profile.bak
[root@localhost ~]# mv /etc/profile.d/* -t /etc/profile.bak/
[root@localhost ~]# mv /etc/bashrc /etc/bashrc.bak
[root@localhost ~]# mv ~/.bash_profile ~/.bash_profile.bak
[root@localhost ~]# mv ~/.bashrc ~/.bashrc.bak

交互式shell,并测试过程如下:

[root@localhost ~]# bash
bash-3.1# LIST
bash: LIST: command not found
bash-3.1# exit
exit
[root@localhost ~]#

显然启动一个普通交互式shell的时候, shell配置文件/etc/profile不起作用

非交互式shell, 测试过程如下:

为了验证先写一个测试脚本,如下:

#!/bin/bash
LIST

把这个脚本保存为t.sh并加下可执行权限:
[root@localhost ~]# chmod a+x t.sh
[root@localhost ~]# ./t.sh       
./t.sh: line 2: LIST: command not found
[root@localhost ~]#
显然启动一个非交互式shell时,shell配置文件/etc/profile不起作用

登录shell,并测试过程如下:
Last login: Wed Nov 19 10:22:23 2008 from 192.168.0.97
-bash-3.1# LIST
total 160
drwxr-xr-x  2 root root  4096 Aug 14 12:24 Desktop
-rw-r--r--  1 root root  3211 Nov  6 10:15 Session.vim
drwxr-xr-x  2 root root  4096 Nov 10 10:58 a
-rw-r--r--  1 root root   126 Nov 12 12:42 a.txt
-rw-r--r--  1 root root   261 Nov  6 15:23 a.zip
-rw-r--r--  1 root root   157 Nov  6 15:23 aa.zip
-rw-------  1 root root  1054 Aug 14 11:59 anaconda-ks.cfg
-rw-r--r--  1 root root   691 Nov 18 10:09 b.txt
-rw-r--r--  1 root root 31671 Aug 14 11:58 install.log
-rw-r--r--  1 root root  4155 Aug 14 11:50 install.log.syslog
-rw-------  1 root root 20310 Nov 17 13:51 mbox
drwxr-xr-x  2 root root  4096 Nov 17 17:22 shell
-rwxrwxrwx  1 root root    65 Nov 19 10:11 t.sh
drwxr-xr-x 14 root root  4096 Nov  5 15:34 test
-bash-3.1#
显然启动一个登录shell时,shell配置文件/etc/profile会起作用

 

~/.bash_profile


这个文件跟/etc/profile起作用的时机是一样的,都是只在启动一个登录shell的时候才会被source,跟/etc/profile不同的是,这里的配置只影响单个用户,不对其它用户产生影响。

 

/etc/bashrc与~/.bashrc
从字面上我们可以理解这两个文件应该跟根bash相关,即只要你启动了一个bash类型的shell这两文件的配置就将发生作用。如果你的shell是sh、csh或是ksh这两个文件将不起作用。按前面的介绍,可能很会猜测/etc/bashrc与~/.bashrc的关系跟/etc/profile与~/.bash_profile的关系一样,一个是全局的,一个是针对单个用户的。从结果上看确实是这样的,但实现过程却是不一样的。启动一个bash时直接source ~/.bashrc, 而这~/.bashrc里面会source /etc/bashrc。

 

/etc/profile.d/*.sh

在fc5下这里的脚本会在/etc/profile里或是~/.bashrc里同时source, 所以这里的设置都是一些不同分类的全局环境设置。

 

 

总结在FC5下一个登录bash的环境初始全过程是:

/etc/profile
    |
    --/etc/profile.d/*
~/.bash_profile
    |
    --~/.bashrc
             |
             --/etc/bashrc
                 |
                 --/etc/profile.d/*

一个普通交互式bash的初始全过程是:
~/.bashrc
    |
    --/etc/bashrc
       |
       --/etc/profile.d/*

对于非交互式bash的初始全过程是:
 不重新source 任何新的shell脚本,只继承当前shell的设置。

抱歉!评论已关闭.