Shell 初始化文件和注销文件
2016-09-17 14:10
387 查看
① 初始化文件被执行的时机
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 filelogin 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
相关文章推荐
- shell的初始化文件:/etc/profile和/etc /bashrc(bash.bashrc)
- shell初始化时与配置有关的几个文件
- 各种shell的初始化文件列表
- shell 初始化二进制文件
- linux环境: shell初始化文件, for TCSH, CSH
- shell的初始化文件:/etc/profile和/etc/bashrc(bash.bashrc)
- Unix&Linux大学教程:14 使用shell:初始化文件
- 分析源代码编译链接过程和shell中ELF格式可执行文件的初始化执行过程
- Android 7.0 init.rc 执行shell脚本 ---- 制作一初始化配置文件
- shell的初始化文件:/etc/profile和/etc /bashrc(bash.bashrc)
- shell的初始化文件:/etc/profile和/etc /bashrc(bash.bashrc)
- shell的初始化文件:/etc/profile和/etc/bashrc(bash.bashrc)
- 理解Linux中Shell初始化文件和用户Profiles文件
- shell判断文件,目录是否存在或者具有权限
- 未能初始化 AppDomain:/LM/W3SVC/1/Root,未能加载文件或程序集“System.Web....”的解决方案
- ORACLE基础学习(初始化文件配置)
- 使用shell脚本进行服务器系统监控——文件系统监控(5)
- Visual Style中的shellstyle.dll文件修改方法
- 不同配置文件的初始化Hibernate心得
- shell里逐行读文件的脚本