The Linux Command Line-WILLIAM(6)-环境

369 阅读5分钟

1 环境(THE ENVIRONMENT)

在shell会话中,shell维护了一个称为环境(environment)的信息体。程序通过使用存储在环境中的信息来决定配置。 尽管大部分程序使用配置文件方式来存储程序设置,也一些程序从环境中读取配置。下面是与环境有关的命令:

  • printenv —Print part or all of the environment.

  • set —Set shell options.

  • export —Export environment to subsequently executed programs.

  • alias —Create an alias for a command.

1.1 环境中的存储

shell在环境(environment)中存储了两种基本类型的数据,尽管在bash中,这两种类型在很大程度上是不可区分的。分别为:环境变量(environment variables)与shell 变量(variables)。

Shell变量是bash放置在其中的一部分数据,而环境变量基本上是其他所有数据。 除了变量之外,shell还存储一些程序数据,即 aliasshell 函数( shell functions)。

1.2 检查环境变量

可以使用bash中内置的 set 或者 printenv 命令检查环境变量。set 命令会展示 shell 以及环境的变量,然而 printenv 只会展示后者。因为环境变量有很多,因此可以使用管道进行处理。

[root@izuf67yuy6secatlp4sztez ~]# printenv | less

结果类似于下面所示:

XDG_SESSION_ID=3659
HOSTNAME=izuf67yuy6secatlp4sztez
TERM=xterm
SHELL=/bin/bash
HISTSIZE=1000
SSH_CLIENT=58.247.180.6 10579 22
SSH_TTY=/dev/pts/0
USER=root
LS_COLORS=rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:mi=01;05;37;41:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arc=01;31:*.arj=01;31:*.taz=01;31:*.lha=01;31:*.lz4=01;31:*.lzh=01;31:*.lzma=01;
...

可以在printenv命令后面加上变量名称:

[root@izuf67yuy6secatlp4sztez ~]# printenv USER
root

set 命令后面没有参数的话默认展示 shell 以及环境变量,也包括定义的shell函数(shell functions):

[root@izuf67yuy6secatlp4sztez ~]# set | less

结果按照字符顺序打印,同时此命令也支持查看特定的变量:

[root@izuf67yuy6secatlp4sztez ~]# echo $HOME
/root

printenv 以及 set都不能查看alias,可以通过 alias 命令来查看:

[root@izuf67yuy6secatlp4sztez ~]# alias
alias cp='cp -i'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'

一些常见的变量:

Table 11-1: Environment Variables

Variable Contents
DISPLAY The name of your display if you are running a graphical environment. Usually this is :0, meaning the first display generated by the X server
EDITOR The name of the program to be used for text editing
SHELL The name of your shell program.
HOME The pathname of your home directory
LANG Defines the character set and collation order of your language
OLD_PWD The previous working directory.
PAGER The name of the program to be used for paging output. This is often set to /usr/bin/less.
PATH A colon-separated list of directories that are searched when you enter the name of an executable program
PS1 Prompt String 1. This defines the contents of your shell prompt. As we will later see, this can be extensively customized
PWD The current working directo
TERM The name of your terminal type. Unix-like systems support many terminal protocols; this variable sets the protocol to be used with your terminal emulator.
TZ Specifies your time zone. Most Unix-like systems maintain the computer’s internal clock in Coordinated Universal Time (UTC) and then display the local time by applying an offset specified by this variable
USER Your usernam

2 如何建立环境

当登录到系统时候,bash程序开始并且读取一系列设置脚本(startup files),这些脚本定义了所有用户共享的默认环境。然后启动并读取此用户home文件夹中定义的个人环境。确切的顺序取决于正在启动的shell会话的类型。

Shell会话有两种:登录Shell会话非登录Shell会话

登录shell会话是一个提示我们输入用户名和密码的会话。 例如,当我们启动虚拟控制台会话时。 当我们在GUI中启动终端会话时,通常会发生非登录Shell会话。

登录shells读取一个或多个启动文件(artup files),如表11-2所示。

Table 11-2: Startup Files for Login Shell Sessions

File Contents
/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.

非登录shell会话读取启动文件,如表11-3所示:

Table 11-3: Startup Files for Non-Login Shell Sessions

File Contents
/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.

除了读取上面的启动文件之外,非登录shell还从它们的父进程(通常是登录shell)继承环境。 从普通用户的角度来看,~/.bashrc文件可能是最重要的启动文件,因为它频繁的读取。 非登录外壳程序默认情况下会读取它,并且大多数登录shells程序的启动文件都是以读取~/.bashrc文件的方式编写的。

2.1 启动文件中有什么?

执行[root@izuf67yuy6secatlp4sztez ~]# less .bash_profile 命令:

# .bash_profile

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

# User specific environment and startup programs

PATH=$PATH:$HOME/bin

export PATH

开头的行是注释,shell不会读取。

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

上面代码被称为if复合命令(if compound command)。可以转换成自然语言来描述:

If the file "~/.bashrc" exists, then read the "~/.bashrc" file.

此段代码说明了登录shell如何获得.bashrc文件内容。

shell如何知道在那里发现命令,比如输入命令ls,shell并没有在整个计算机内搜索/bin/ls(ls的完全文件名),而是搜索 PATH 变量中包含的路径。

PATH 变量通常由 /etc/profile 启动文件设置(但并非总是如此,取决于发行版),由以下代码设置:

PATH=$PATH:$HOME/bin

修改 PATH 来增加$HOME/bin路径到列表末端。这是参数扩展的示例。示例命令如下所示:

[me@linuxbox ~]$ foo="This is some"
[me@linuxbox ~]$ echo $foo
This is some
[me@linuxbox ~]$ foo=$foo" text."
[me@linuxbox ~]$ echo $foo
This is some text.

这样就可以将字符串追加到变量的末端。

通过将字符串$HOME/bin添加到PATH变量内容的末尾,搜索命令的时候也会在$HOME/bin路径下搜索。这意味着,当我们想要创建自己的脚本命令时,可以在$HOME/bin路径下创建然后直接在命令行中调用就可以了。 注:默认情况下,许多发行版都提供此PATH设置。 一些基于Debian的发行版(例如Ubuntu)在登录时测试~/bin目录的存在,如果找到该目录,则将其动态添加到PATH变量中。

export PATH

export 命令告诉shell将PATH的内容提供给该shell的子进程。

2.2 修改环境

由于知道了启动文件在哪里以及它们包含什么,可以对其进行修改以自定义环境。

2.2.1 应该修改哪些文件?

通常,要将目录添加到PATH或定义其他环境变量,请将这些更改放置在.bash_profile中(或根据系统版本情况进行等效更改,例如,Ubuntu使用.profile)。 对于其他所有内容,将更改放在.bashrc中。 除非为系统管理员,并且需要更改系统所有用户的默认设置,否则,不要对主目录中的文件进行修改。 当然可以更改/ etc中的文件(例如配置文件)。

文本编辑器(text Editor) 为了编辑文本要使用文本编辑器。文本编辑器就像文字处理程序一样,允许移动光标在屏幕上编辑文本。 常用的文本编辑器有nano,vi(在大多数Linux系统上,已被名为vim(Vi Improved)的程序代替)以及emacs。

使用 文本编辑器(text Editor) 在使用文本编辑器编辑重要配置文件时候首先备份。

[me@linuxbox ~]$ cp .bashrc .bashrc.bak

备份文件名称叫什么无所谓,常见的扩展有.bak, .sav, .old 以及 .orig,值得注意的是cp命令会沉默的覆盖已经存在的文件。

[me@linuxbox ~]$ nano .bashrc

# .bashrc

# User specific aliases and functions

alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'

# Source global definitions
if [ -f /etc/bashrc ]; then
        . /etc/bashrc
fi


                                    [ Read 12 lines ]
^G Get Help   ^O WriteOut   ^R Read File  ^Y Prev Page  ^K Cut Text   ^C Cur Pos
^X Exit       ^J Justify    ^W Where Is   ^V Next Page  ^U UnCut Text ^T To Spell

该屏幕由顶部的标题、中间编辑的文件文本和底部的命令菜单组成。由于nano的设计目的是取代电子邮件客户端提供的文本编辑器,所以它的编辑功能相当缺乏。

在任何文本编辑器中都应该学习的第一个命令是如何退出程序。对于nano,按CTRL-X退出。 nano中CTRL-O保存我们的工作。 有了这些知识,我们就可以进行一些编辑了。 使用向下箭头键和/或向下翻页键,将光标移动到文件的末尾,然后将以下行添加到.bashrc文件中

umask 0002
export HISTCONTROL=ignoredups
export HISTSIZE=1000
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'

Table 11-4: Additions to Our .bashrc File

Line Meaning
Umask 0002 Sets the umask to solve the problem with shared directories we discussed in Chapter 9
export HISTCONTROL=ignoredups Causes the shell’s history recording feature to ignore a command if the same command was just recorded.
export HISTSIZE=1000 Increases the size of the command history from the default of 500 lines to 1000 lines.
alias l.='ls -d .* --color=auto' Creates a new command called l., which displays all directory entries that begin with a dot.
alias ll='ls -l –color=auto' Creates a new command called ll, which displays a long-format directory listing.

可以使用#符号来添加注释:

# Change umask to make directory sharing easier
umask 0002
# Ignore duplicates in command history and increase
# history size to 1000 lines
export HISTCONTROL=ignoredups
export HISTSIZE=1000
# Add some helpful aliases
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'

2.2.2 使我们的改变生效

因为.bashrc文件仅在会话开始时读取,我们对.bashrc所做的更改只有在关闭终端会话并开始新会话后才会生效。但是,我们可以使用以下命令强制bash重新读取经过修改的.bashrc文件:

[me@linuxbox ~]$ source .bashrc

执行命令后,可以看到所做的更改生效,可以验证下:

[root@izuf67yuy6secatlp4sztez ~]# ll
total 404
-r-x------ 1 root root    189 Mar 23 11:37 env.txt
-rw-r--r-- 1 root root 402641 Apr  5 13:10 sa_recovery.log