Shell环境(environment)和配置(configuration)

Shell在shell会话过程中维系的信息叫做环境(environment)。环境中被程序使用的数据决定着我们的配置(configuration)。尽管大多数程序使用配置文件(configuration files)保存程序的设置,一些程序也会查找环境中保存的值来调整它们的行为。

虽然对bash来说这两种数据很难区分,Shell在环境中保存两种数据:环境变量(environmental variables)和shell变量(shell variables)。

相关命令:

printevn:显示环境变量(environment variables)。

set:显示shell变量(shell variables)和环境变量(environment variables)。

export:将shell变量导出到环境中,可以使后续执行的程序(子进程)使用。

unset:销毁shell变量或环境变量。 注:在bash中没有将变量从环境中移出的命令,只能销毁。

set -o:打开shell选项。

set +o:关闭shell选项。

alias:给命令创建别名 (create an alias for a command)。格式为:alias name='string'

unalis: 取消别名。格式为:unalias name

\command: 临时取消名为command的别名。格式为:\command

source:也称为“点命令”,也就是一个点符号(.)。source命令通常用于重新执行刚修改的初始化文件,使之立即生效,而不必注销并重新登录。

环境的建立

     当我们登陆系统的时候,bash启动并读取一系列叫做startup files的配置脚本。这些脚本定义了被所有用户共享的默认环境。之后,bash读取在我们用户主目录(home directory)中的startup files。这部分脚本文件定义了我们用户的个人环境。具体的顺序取决于启动的shell会话类型。

  Login 和 Non-login Shell:

 Shell可以分为交互式和非交互式。当在shell提示符下输入命令时使用的是交互式shell;运行shell脚本时使用的是非交互式shell。然而交互式shell又分为两种:Login Shells和Non-login Shells。 Login shell会话就是那些在开始时提示你需要用户名和密码的shell会话。Non-login shell会话的一个典型例子就是在GUI开启的终端会话。

    

Startup Files for Login Shell Sessions (LOGIN FILES)
/etc/profile A global configuration script that applies to all users.
~/.bash_profile

A user’s personal startup file. Can be used to extend or override settings

in the global configuration script.

~/.bash_login

If ~/.bash_profile is not found, bash attempts to read this script.

~/.profile

If neither ~/.bash_profile nor ~/.bash_login is found, bash attempts to

read this file. This is the default in Debian-based distributions, such as Ubuntu.

 

Startup Files for Non-Login Shell Sessions (ENVIRONMENT FILES)
/etc/bash.bashrc A global configuration script that applies to all users.
~/.bashrc

A user’s personal startup file. Can be used to extend or override settings

in the global configuration script.

这些都叫做INITIALIZATION FILES. LOGIN FIlE中存放每次登陆都想要执行的命令。ENVIRONMENT FILE中存放每次开启新shell都想要执行的命令。这些都是一些可以根据自己需要定制的文件。有一些shell还提供LOGOUT FILE来存放退出系统时希望自动执行的命令。

POSIX标准强制shell必须支持login file和environment file,但不对logout file作必需要求。此外要求有一个环境变量ENV来保存environment file的文件名。另外,login shell需要执行login file和environment file。Non-login shell只需要执行environment file。

但是默认模式下的Bash并不遵从POSIX标准,它的login shell只执行login file,但是我们可以在它的login file中发现其调用执行 environment file的语句。

初始化文件所存内容

LOGIN FILES:主要搭建起环境和初始化工作会话。所以应该包含1)创建和修改环境变量的命令2)执行一次性的动作。

ENVIRONMENT FILES:存放不能保存在环境中的设置,特别是 shell options、alias和functions。这些都不能再环境中长久保存,所以每当新shell启动都需要重新创建。

posted @ 2014-01-06 19:31  purgiant  阅读(1358)  评论(0编辑  收藏  举报