bash 类别简述

简介: <p>本文内容来自man bash。</p> <h2>一. bash分类<br></h2> <p>bash从不同角度看分为两种,一种是是否登录(login),另一种是是否交互式(interactive)。</p> <p>登录式shell是启动时:</p> <p>1. 第0个参数以“-”开头</p> <p>2. 或使用--login选项</p> <p>交互式shell是启动时:

本文内容来自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
目录
相关文章
|
4月前
|
监控 安全 Shell
防止员工泄密的措施:在Linux环境下使用Bash脚本实现日志监控
在Linux环境下,为防止员工泄密,本文提出使用Bash脚本进行日志监控。脚本会定期检查系统日志文件,搜索敏感关键词(如&quot;password&quot;、&quot;confidential&quot;、&quot;secret&quot;),并将匹配项记录到临时日志文件。当检测到可疑活动时,脚本通过curl自动将数据POST到公司内部网站进行分析处理,增强信息安全防护。
164 0
|
4月前
|
Linux Shell Windows
4:Bash shell命令-步入Linux的现代方法
4:Bash shell命令-步入Linux的现代方法
81 0
|
Ubuntu 安全 Linux
不用安装虚拟机,直接在Windows上面运行Linux Bash Shell,嗯!真香!!!
不用安装虚拟机,直接在Windows上面运行Linux Bash Shell,嗯!真香!!!
231 0
|
11月前
|
关系型数据库 MySQL Shell
【Linux命令】-bash: mysql: command not found
【Linux命令】-bash: mysql: command not found
125 0
|
4月前
|
存储 Shell Linux
Linux Bash 脚本中的 IFS 是什么?
【4月更文挑战第25天】
97 0
Linux Bash 脚本中的 IFS 是什么?
|
2月前
|
存储 Shell Linux
Linux|创建和使用 Bash 别名
Linux|创建和使用 Bash 别名
38 6
|
4月前
|
Java Shell Linux
【linux进程控制(三)】进程程序替换--如何自己实现一个bash解释器?
【linux进程控制(三)】进程程序替换--如何自己实现一个bash解释器?
|
4月前
|
Shell Linux
【Linux】Bash支持各种指令选项的原理:命令行参数
【Linux】Bash支持各种指令选项的原理:命令行参数
|
4月前
|
存储 Shell Linux
【攻防世界】unseping (反序列化与Linux bash shell)
【攻防世界】unseping (反序列化与Linux bash shell)
|
4月前
|
Linux Shell 开发工具
linux】-bash:vim:未找到命令
linux】-bash:vim:未找到命令
73 0