.bash_profile、.bash_logout和.bashrc文件的区别

最重要的bash文件是.bash_profile,它在用户每次登陆到系统时被读取,其中包含的命令被bash执行。如果修改其中的内容,注意直到退出并再次登陆,该文件被重新读取后,你想.bash_profile键入的内容才会生效。或者,可以使用source命令,例如,

source .bash_profile

.bash_profile只被登陆shell读取并执行。如果你通过在命令行上键入bash启动一个新的shell,它就会试图读取.bashrc中的命令。这种策略给出将登陆时需要的启动命令和运行一个子shell所需的命令分离开的灵活性。如果你需要在登陆shell和启动一个子shell命令时进行一样的操作,可以在.bash_profile中使用source命令执行.bashrc。如果.bashrc不存在,那么启动一个子shell时就没用命令执行,

文件.bash_logout在每次登陆shell退出时被读取并执行。它提供了定制用户环境的功能,如果要执行诸如删除账号内临时文件或记录登录系统所花时间等命令,则可以将这些命令放在.bash_logout内,该文件不必一定存在账号内-----如果不存在,则退出时不再执行其他命令。


作为一条规则:应该将尽可能少的定义放在.bash_profile中,而将尽可能多的定义放在环境文件.bashrc中,吟哦无奈这些定义是加入到环境而不是移走,那么对于子进程的工作,出错的几率就会大大减小(例外情况是过分使用别名引起的名称冲突),选项和别名定义都可以放入环境文件。

The content of .bashrc may be something like the following:


# .bashrc
# User specific aliases and functions
# Source global definitions

if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi

# User specific environment and startup programs
PATH=$PATH:$ORACLE_HOME/bin:/usr/local/java/bin
CDPATH=$CDPATH:/playgnd/mg/scripts:/~/Scripts


BASH_ENV=$HOME/.bashrc
USERNAME=Ghodrat
HISTSIZE=100

export BASH_ENV USERNAME

#The following setting prohibits file overwriting
set -o noclobber

#The following setting ignores ctrl+ d and does not
#end the session
set -o ignoreeof

#The following setting let's special characters work as
#expected
set +o noglob


## some usefull aliases
alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
alias ls='ls -F'



2.5.1.2 .bash_profile File
Contents

The .bash_profile is a hidden file which resides in user's
home directory and defines our processing environment when we
login. The .bash_profile is executed when we login and,
among other details, establishes such things as home directory,
search path, etc.

The content of .bash_profile may look like the following:


# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi

# User specific environment and startup programs
echo "Running .bash_profile..."
date




2.5.1.2 .bash_logout File
Contents

The .bash_logout file is a hidden file which resides in
user'shome directory and is executed when logging out of a linux
system running the BASH shell . This file is usually very short,
and contains commands users want to execute upon leaving the
account. The most popular is the clear command, which clears the
screen.


#Running ~/.bash_logout
echo "Running .bash_logout..."

#USERNAME must have been defined as an environment variable
echo Bye $USERNAME



你可能感兴趣的:(linux)