Shell 初始化文件和注销文件

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

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: .bashrc
Bash (POSIX mode)
   • Login shell: .profile, then $ENV
   • Non-login shell: $ENV
Korn 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: .cshrc
Tcsh
   • 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 variables
export HISTSIZE=500
export PATH="${PATH}:${HOME}/bin"
export VISUAL=vi

# 2A. Shell prompt - Bash
export PS1="(\w) `basename ${SHELL}`[\!]$ "
# 2B. Shell prompt - Korn Shell
export PS1="(\$PWD) `basename ${SHELL}`[!]$ "

# 3. File creation mask
umask 077

# 4. Terminal settings (for remote host only)
stty erase ^H
# stty erase ^?

# 5. Display welcome message
echo "Welcome $LOGNAME."
echo "Today is `date`."
echo

# 6. System information
echo "Last three logins:"; last `logname` | head -3
echo
echo "Current users: `users`"
echo
echo "System uptime:"; uptime
echo

#7.define preferred tools
export EDITOR=vim
export PAGER=less 

# 7A. Environment file - Bash
#--正常来说,默认下的登陆shell是不执行环境文件的!
#这里通过条件判断来使得环境文件得以执行!
if [ -f ${HOME}/.bashrc ]
then source ${HOME}/.bashrc
fi
# 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 options
set -o ignoreeof
set -o emacs
set -o noclobber

# 2. Aliases
alias a=alias
alias del='fc -s ls=rm'
alias h=history
alias 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 definitions
if [ -f /etc/bashrc ]; then
        . /etc/bashrc
fi
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值