8.6. Shell 环境

命令解译器 (或 shells) 是用户接触电脑的第一个点,必须足够友善。大部分的人使用初始设置的脚本配置其行为 (自动完成、提示字符等)。

bash 是标准的 shell,使用 /etc/bash.bashrc 初始脚本做为 “交互” shells,并以 /etc/profile 做为 “登录” shells。

基本 登录 shell 与 (非) 交互 shell

简单说,不论本地登录或使用 ssh 远程登录,或以 bash --login 命令登录,都启用登录 shell。在 登录或非登录 shell 里,该 shell 必须是交互的 (例如在 xterm-type 终端机内);或非交互式 (使用运行式脚本)。

发现 其他 shells、其他脚本

Each command interpreter has a specific syntax and its own configuration files. Thus, zsh uses /etc/zshrc and /etc/zshenv; tcsh uses /etc/csh.cshrc, /etc/csh.login and /etc/csh.logout. The man pages for these programs document which files they use.

bash 启用 /etc/bash.bashrc 文件 (未评论的几列) 内的 “自动完成”。

基本 自动补完

若干命令解译器提供补完功能,即键入命令前几个字后,用户按 Tab 后,shell 自动补完命令名称或参数。用户的工作更有效率且更少错误。

This function is very powerful and flexible. It is possible to configure its behavior according to each command. Thus, the first argument following apt will be proposed according to the syntax of this command, even if it does not match any file (in this case, the possible choices are install, remove, upgrade, etc.).

The package bash-completion contains completions for most common programs.

基本 波浪号,回到家文件夹的捷径

波浪号通常用于表示环境变量 HOME 指向 (就是用户的家文件夹,如 /home/rhertzog/) 的文件夹。命令解译器自动取代:~/hello.txt 就是 /home/rhertzog/hello.txt

波浪号也可近用至另个用户的家文件夹。因此,~rmas/bonjour.txt 等于 /home/rmas/bonjour.txt

除了这些常见的脚本,用户还可以创建自己的 ~/.bashrc~/.bash_profile 来配置其 shell。常见的改变是添加别名;即自动取代运行的命令,以加速命令的效率。例如,添加 la 别名取代 ls -la | less 命令后,只需键入 la 就能查看文件夹内容的详情。

基本 环境变量

环境变量保存供 shell 或其他程序使用的全域设置。它们是与环境相关的 (每个进程都有自己的环境变量) 但可继承的。最后这个特性让登录 shell 时可以声明继承所有程序的变量。

设置缺省的环境变量是 shell 配置的重要工作。除了把变量指定给 shell 之外,还可以放在 /etc/environment 文件内,以备供启始 shell 的其他程序使用。设置的变量通常包括 ORGANIZATION,包括公司或组织的名称;以及 HTTP_PROXY,指示 HTTP 代理服务器的位置。

秘诀 所有的 shells 拥有相同的配置

用户通常以相同的方式登录与交互 shell。所以,从 ~/.bashrc 里的 ~/.bash_profile 文件选择解译器 (或 “来源”) 的内容。也可使用适用于所有用户的文件 (调用 /etc/bash.bashrc 源自 /etc/profile)。