.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



  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值