Bash的启动文件

Bash的启动文件

/bin/bash这个命令解释程序(后面简称shell)使用了一系列启动文件来建立一个运行环境。每一个文件都有特殊的功用并对登陆和交互环境有不同的影响。

一个交互的登陆shell会在 /bin/login 成功登陆之后运行。一个交互的非登陆shell是通过命令行来运行的,如[prompt]$/bin/bash。一般一个非交互的shell出现在运行shell脚本的时候。之所以叫非交互的shell,是因为它不在命令行上等待输入而只是执行脚本程序。

通过info bash 小节: Bash Startup Files and Interactive Shells,可以得到更多有关bash启动文件的信息。

下列文件被用来确定在shell调用的时候读取了正确的环境: /etc/profile, /etc/bashrc, ~/.bash_profile~/.bashrc~/.bash_logout会在shell启动的时候被用到,而是在用户注销登陆的时候被读取。 /etc/profile~/.bash_profile 是在启动一个交互登陆shell的时候被调用。~/.bashrc 是在一个交互的非登陆shell启动的时候被调用。

下面是一个基本的/etc/profile。具体说明见文件中的注释。如果想要得到有关提示符换码符序列更多的信息(例如:PS1环境变量),可以阅读 info bash -- 小节:Printing a Prompt.

# /etc/profile开始
# Written for Beyond Linux From Scratch
# by James Robertson <jameswrobertson@earthlink.net>

# 文件包含系统全局变量和初始程序

# 系统全局的别名(aliases)和函数应该包含于/etc/bashrc. 个人
# 的环境变量和初始程序应该在~/.bash_profile中  个人的别名和
# 函数设置应该放在 ~/.bashrc里面。

# 用来管理路经的函数
pathman () {
	if ! echo $PATH | /bin/egrep -q "(^|:)$1($|:)" ; then
		if [ "$2" = "last" ] ; then
			PATH=$PATH:$1
		else
			PATH=$1:$PATH
		fi
	fi
}

# 添加到标准路经
if [ $(id -u) = 0 ] ; then
	if [ -d "/usr/local/sbin" ] ; then
		pathman /usr/local/sbin last
	fi
fi

if [ $(id -u) != 0 ] ; then
	if [ -d "/usr/local/bin" ] ; then
		pathman /usr/local/bin last
	fi
fi

if [ -d "/usr/X11R6/bin" ] ; then
	pathman /usr/X11R6/bin last
fi

# 设定一些环境变量
HISTSIZE=1000
PS1="[/u@/h /w]//$ "

# 设定 INPUTRC 变量
if [ -z "$INPUTRC" -a ! -f "$HOME/.inputrc" ] ; then
	INPUTRC=/etc/inputrc
fi

# 设置 /bin/ls 支持颜色,其别名放在 /etc/bashrc中
if [ -f "/etc/dircolors" ] ; then
	eval $(dircolors -b /etc/dircolors)

	if [ -f "$HOME/.dircolors" ] ; then
		eval $(dircolors -b $HOME/.dircolors)
	fi
fi

export PATH HISTSIZE PS1 LS_COLORS INPUTRC

# /etc/profile 结束

下面是一个基本的/etc/bashrc。具体说明见文件中的注释。

# /etc/bashrc开始
# Written for Beyond Linux From Scratch
# by James Robertson <jameswrobertson@earthlink.net>

# 文件中设置系统全局别名(aliases)和函数

# 系统全局变量和初始程序包含于 /etc/profile。  个人
# 的环境变量和初始程序应该在~/.bash_profile中  个人的别名和
# 函数设置应该放在 ~/.bashrc里面。

# 作为缺省使用 umask 来获取设置
# 对于非互动和非启动shell都是这样。
if [ "$(id -gn)" = "$(id -un)" -a $(id -u) -gt 99 ] ; then
	umask 002
else
	umask 022
fi

# 设置 /bin/ls 为有颜色的列表。配合
# /etc/profile里面的设置一起起作用。
alias ls='ls --color=auto'

# /etc/bashrc结束

下面是基本的 ~/.bash_profile。具体说明见文件中的注释。

# ~/.bash_profile 开始
# Written for Beyond Linux From Scratch
# by James Robertson <jameswrobertson@earthlink.net>

# 文件中设置个人环境变量和启动程序。

# 系统全局变量和初始程序包含于/etc/profile
# 系统全局的别名(aliases)和函数应该包含于/etc/bashrc。个人的别名和
# 函数设置应该放在 ~/.bashrc里面。

if [ -f "$HOME/.bashrc" ] ; then
	source $HOME/.bashrc
fi

if [ -d "$HOME/bin" ] ; then
	pathman $HOME/bin last
fi

export PATH

# ~/.bash_profile 结束

下面是基本的~/.bashrc。具体说明见文件注释

# ~/.bashrc 开始
# Written for Beyond Linux From Scratch
# by James Robertson <jameswrobertson@earthlink.net>

# 文件中包含个人的别名和函数设置

# 个人环境变量和启动程序在 ~/.bash_profile中设置。
# 文件包含系统全局变量和初始程序包含于/etc/profile
# 系统全局的别名(aliases)和函数应该包含于/etc/bashrc。

if [ -f "/etc/bashrc" ] ; then
	source /etc/bashrc
fi

# ~/.bashrc结束

下面是基本的~/.bash_logout。具体说明见文件注释。你会发现~/.bash_logout 并不包含 clear 命令。这个是因为清屏的工作是由/etc/issue 完成的。

# ~/.bash_logout 开始
# Written for Beyond Linux From Scratch
# by James Robertson <jameswrobertson@earthlink.net>

# 个人希望在注销登陆时作的工作。

# ~/.bash_logout 结束

如果您希望使用在/etc/profile中调用的 /etc/dircolors 或者~/.dircolors,可以执行下面的命令: /bin/dircolors -p > /etc/dircolors 或者 /bin/dircolors -p > ~/.dircolors。在/etc中的文件应该被用于全局设置,如果一个同样的设置文件在用户的$HOME目录中这个文件将优先于etc中的设置文件,同时etc中的文件将不被读取。建议在/etc/skel建立一个基本的.dircolors文件,作为每一个新用户的缺省设置。

Ian Macdonald 写过一个有关加强shell环境功能的小技巧的文章。可以在这里找到: http://www.caliban.org/bash/index.shtml

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值