Shell 初始化文件和注销文件

来源:互联网 发布:sql中as的用法 编辑:程序博客网 时间:2024/05/22 07:58

① 初始化文件被执行的时机

Which initialization files are executed and when?
There are two general rules, with minor variations.

     1.A login shell executes your login file and your environment file.     2.A non-login shell only executes your environment file.

B-shell家族:

Bash (default mode)   • Login shell: .bash_profile   • Non-login shell: .bashrcBash (POSIX mode)   • Login shell: .profile, then $ENV   • Non-login shell: $ENVKorn shell   • Login shell: .profile, then $ENV   • Non-login shell: $ENV

1.默认模式的bash,login shell不会执行环境文件。

it is only the Bash(default mode) login shell that executes a login file and not an environment file.This means that Bash users must put a special command in their login file to force it to execute the environment file.

2.korn shell必须让login file先执行,以设置好 $ENV 环境变量,否则找不到 环境文件.

"Korn had to ensure that the login file executed before the environment file. Otherwise, there would be no way for a user to set the ENV variable."

3.bash 以POSIX模式运行: bash --posix


C-shell家族:

C-Shell   • Login shell: .cshrc, then .login   • Non-login shell: .cshrcTcsh   • Login shell: .tcshrc, then .login   • Non-login shell: .tcshrc(For backwards compatibility,if the Tcsh can’t find .tcshrc,it will look for .cshrc.)

两个shell家族的运行时机区别:

In the C-Shell family, login shells execute the environment file first. In the Bourne shell family,login shells execute the login file first. 历史原因:Joy developed the C-Shell at U.C. Berkeley, he enhanced the initialization process by using two files instead of one. The first file, .cshrc, ran every time a new shell started. The second file, .login, ran only when a login shell started. Thus, it made sense to execute .login after .cshrc, as its job was to run only those extra commands that were necessary at login time.the Korn shell,Korn adopted Bill Joy’s idea of using two initialization files, what we now call an environment file (.cshrc) and a login file (.login)Because Korn worked at Bell Labs, which was a Bourne shell shop, he used the name .profile for the login shell.  When it came time to name the environment file,Korn decided to let the users choose the name for themselves,by setting the ENV variable to the name of the environment shell.

–附图:
这里写图片描述


② B-shell的初始化文件示例

1.login file
login file has two jobs:
to set up your environment and to initialize your work session.
Thus, your login file should contain commands to

 (1) create or modify environment variables (2) perform all one-time actions.
# ======================================# Bourne Shell family: Sample login file# ======================================# 1. Environment variablesexport HISTSIZE=500export PATH="${PATH}:${HOME}/bin"export VISUAL=vi# 2A. Shell prompt - Bashexport PS1="(\w) `basename ${SHELL}`[\!]$ "# 2B. Shell prompt - Korn Shellexport PS1="(\$PWD) `basename ${SHELL}`[!]$ "# 3. File creation maskumask 077# 4. Terminal settings (for remote host only)stty erase ^H# stty erase ^?# 5. Display welcome messageecho "Welcome $LOGNAME."echo "Today is `date`."echo# 6. System informationecho "Last three logins:"; last `logname` | head -3echoecho "Current users: `users`"echoecho "System uptime:"; uptimeecho#7.define preferred toolsexport EDITOR=vimexport PAGER=less # 7A. Environment file - Bash#--正常来说,默认下的登陆shell是不执行环境文件的!#这里通过条件判断来使得环境文件得以执行!if [ -f ${HOME}/.bashrc ]then source ${HOME}/.bashrcfi# 7B. Environment file - Korn Shell#这里 $ENV 是可以指向任何的脚本export ENV=${HOME}/.kshrc# 8. Logout file - Korn Shell#the Korn shell does not support a logout file, #the file that is executed automatically each time you log out.# However, you can simulate a logout file by trapping the EXIT signal, which is generated when you logout. trap '. ${HOME}/.logout; exit' EXIT

2.ENV file:主要是重新执行新的shell运行时,会丢失的环境配置。

#With the Bourne shell family, the environment file is simpler than the login file, #because the login file does most of the work.#All the environment file needs to do is recreate whatever is lost when a new shell is started: #shell options, aliases and functions."environment file has a different job: to set up whatever customizations cannot be stored in the environment, in particular, shell options, aliases and functions. ""With Bash, your environment file is named .bashrc. With the Korn shell or Bash in POSIX mode,   you can name the environment file anything you want by setting the ENV variable. My suggestion is to use .kshrc for the Korn shell and .bashrc for Bash."# ============================================|# Bourne Shell family: Sample environment file|# ============================================|# 1. Shell optionsset -o ignoreeofset -o emacsset -o noclobber# 2. Aliasesalias a=aliasalias del='fc -s ls=rm'alias h=historyalias l='ls -F'alias la='ls -a'alias ll='ls -l'alias r='fc -s'alias cl=clear#阻止刷新屏幕alias less='less -X'#3. Functions# functions go here#4.linux下甚至会执行/etc/bashrc配置文件# Source global definitionsif [ -f /etc/bashrc ]; then        . /etc/bashrcfi
0 0