bash 类别简述

本文内容来自man bash。

一. bash分类

bash从不同角度看分为两种,一种是是否登录(login),另一种是是否交互式(interactive)。

登录式shell是启动时:

1. 第0个参数以“-”开头

2. 或使用--login选项

交互式shell是启动时:

1. 没有非选项参数除非用使用了-s选项;

2. 并且没有-c选项;

3. 并且input/outptu与terminal相连;

4. 并且用-i选项


两种不同分法的shell有4种组合方式:

1. 交互登录式shell

2. 交互非登录式shell

3. 非交互登录式shell

4. 非交互非登录式shell


二. bash启动加载文件

1. 登录式shell,不管是不是交互的

(如果相应文件不存在就跳过)

/etc/profile
~/.bash_profile
~/.bash_login
~/.profile

退出时会执行:

~/.bash_logout

2. 交互非登录式shell

/etc/bash.bashrc
~/.bashrc

3. 非交互非登录模式

在执行脚本时,回去找BASH_ENV这个环境变量,并用这个环境变量的值作为脚本名执行


三. 总结:

我的系统是debian,查看相应文件可以看到

head -n1 ~/.profile
~/.profile: executed by the command interpreter for login shells.
head -n1 ~/.basrc
~/.bashrc: executed by bash(1) for non-login shells


一般来说,在~/.profile中会去source ~/.bashrc,也就是说我们平时配置环境是,直接在~/.bashrc中配置即可。


最后说一下如何检测当前shell是否为交互式的

 determine within a startup script whether or not Bash isrunning interactively,test the value of the ‘-’ special parameter.It containsi when the shell is interactive. For example:

case "$-" in
*i*)	echo This shell is interactive ;;
*)	echo This shell is not interactive ;;
esac


Alternatively, startup scripts may examine the variablePS1; it is unset in non-interactive shells, and set ininteractive shells. Thus:

if [ -z "$PS1" ]; then
        echo This shell is not interactive
else
        echo This shell is interactive
fi


参考:
http://www.gnu.org/software/bash/manual/html_node/Is-this-Shell-Interactive_003f.html#Is-this-Shell-Interactive_003f
http://www.gnu.org/software/bash/manual/html_node/Bash-Startup-Files.html
http://blogread.cn/it/article/6007?f=sa
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值