初识Linux .bash_profile, .bash_logout, and .bashrc 文件

   Linux的.bash_profile, .bash_logout, .bashrc 三个文件对 bash 来说有特殊的意义。你可以通过修改这三个文件让系统自动为你的账户进行个性化环境的设置。这些文件可能存在于你的主目录下面,如果不存在,那么系统将根据/etc/profile文件进行配置。
  .bash_profile非常重要,你每次登录shell的时候都要去读这个文件(并且只在登录时读取)。查看该文件,可能是如下内容:
   PATH=/sbin:/usr/sbin:/bin:/usr/bin:/usr/local/bin
   SHELL=/bin/bash
   MANPATH=/usr/man:/usr/X11/man
   EDITOR=/usr/bin/vi
   PS1='\h:\w\$ '
   PS2='> '
   export EDITOR

   你可以用vi或者emacs对其进行编辑(建议不要更改原来的内容,只要把你要写的内容添加在后面就行,或者在进行编辑之前对其进行备份)。修改完并不会立即生效,系统必须re-read一下才行。你可以退出并重新登录系统,或者用source .bash_profile命令使之生效。
  .bash_logout相反,每次退出shell的时候才去读这个文件(并且只在退出时读取)。如果你需要在退出系统的时候执行一些命令,比如清除一些临时文件,清除一些日志,你可以把命令写入该文件。该文件并不是必须存在的,当你发现你的账号没有这个文件,那么意味着你退出系统的时候,没有指定的额外的命令需要执行。
  各种版本的shell命名.bash_profile这个文件的方式有所不同。所以当你看到.bash_login,它就对应前文所提的.bash_profile。如果你使用bash登录,bash首先回去寻找.bash_profile文件,如果该文件不存在,则会自动寻找.bash_login,如果还是不存在,则自动再去寻找 .profile 文件。

   .bashrc,The difference between this script and .bashrc is that .bash_profile runs only when you log in. It was originally designed so that you could separate interactive shells from those run by background processors such as cron.But it is not very useful on modern computers with the X Window System because when you open a new terminal window, only .bashrc runs. If you start up a window with the command xterm -ls, it will run .bash_profile too.

posted on 2009-02-23 09:55  starspace  阅读(8951)  评论(1编辑  收藏  举报

导航