第一章 开始 bash

Chapter 1 Beginning bash

What’s a shell, and why should you care about it?

什么是 Shell(外壳),以及为什么你应当关心它?

译注:你可以称其为外壳,也可以不翻译,直接称其为 Shell。从下文的解释来开,它真的就是个外壳,并非操作系统的一部分。

Any recent computer operating system (by recent, we mean since about 1970) has some sort of user interface—some way of specifying commands for the operating system to execute. But in lots of operating systems, that command interface was really built in and there was only one way to talk to the computer. Furthermore, an operating system’s command interface would let you execute commands, but that was about all. After all, what else was there to do?

所有近代的计算机操作系统(近代的意思是从1970年开始)都具有某种用户接口——一种给操作系统指定命令并执行的方法。但在许多操作系统中,命令接口是内建的,且只有一种与计算机交流的方法。此外,一个操作系统的命令接口可以让你执行命令,但那就是操作系统的全部了。那么,还有什么可做的呢?

The Unix operating system popularized the notion of separating the shell (the part of the system that lets you type commands) from everything else: the input/output system, the scheduler, memory management, and all of the other things the operating system takes care of for you (and that most users don’t want to care about). The shell was just one more program; it was a program whose job was executing other programs on behalf of users.

Unix 操作系统普及了一个概念,即把 Shell 与其他一切分开:输入输出系统、调度进程、存储管理,以及其它任何操作系统替你接管的一切(那些基本都是用户不想关心的)。Shell 只是一个程序;这个程序的任务是替用户执行其他程序。

But that was the beginning of a revolution. The shell was just another program that ran on Unix; if you didn’t like the standard one, you could create your own. So by the end of Unix’s first decade, there were at least two competing shells: the Bourne shell, sh (which was a descendant of the original Thompson shell), plus the C shell, csh. By the end of Unix’s second decade, there were a few more alternatives: the Korn shell, ksh, and the first versions of the bash shell. By the end of Unix’s third decade, there were probably a dozen different shells.

不过这只是变革的开始。Shell 只是运行在 Unix 上的一个程序;若你不喜欢标准的 Shell 程序,你可以自创一个。所以到 Unix 的头十年结束时,至少有两个竞争的 Shell:伯尔尼 Shell,sh(这是原生的汤姆森 Shell 的后代),还有一个 C Shell,csh。到 Unix 的第二个十年结束时,出现了一些替代方案:Korn Shell,ksh,以及第一个版本的 bash Shell。到 Unix 第三个十年结束时,出现了一大堆不同的 Shell。

You probably don’t sit around saying, “Should I use csh or bash or ksh today?” You’re probably happy with the standard shell that came with your Linux (or BSD or macOS or Solaris or HP/UX) system. But disentangling the shell from the operating system itself made it much easier for software developers (such as Brian Fox, the creator of bash, and Chet Ramey, the current developer and maintainer) to write better shells—you could create a new shell without modifying the operating system itself. It was much easier to get a new shell accepted, since you didn’t have to talk some operating system vendor into building the shell into their system; all you had to do was package the shell so that it could be installed just like any other program.

你可能会说,“那我们现在使用 csh、bash 还是 ksh 呢?”你可能对你的 Linux (或 BSD、macOS、Saloris 还是 HP/UX)系统自带的标准 Shell 很满意了。但是把 Shell 与操作系统本身分开,能让软件开发者(如 bash 的作者布莱恩·福克斯,以及 bash 目前的开发者和维护者 Chet Ramey)更容易写出更好的 Shell——你能够在不修改操作系统本身的基础上开发出一个新的 Shell。这样,接受新的 Shell 也更容易了,因为你不必与操作系统供应商讨论将 Shell 构建到它们的系统中;你需要做的全部事情就是像其他程序一样将 Shell 打包为可安装软件包。

Still, you might be thinking that sounds like a lot of fuss for something that just takes commands and executes them. And you would be right—a shell that just let you type commands wouldn’t be very interesting. However, two factors drove the evolution of the Unix shell: user convenience and programming. And the result is a modern shell that does much more than just accept commands.

尽管如此,你可能会认为,对于只接受命令并执行这种事情听上去很麻烦。你这么想也许有一定道理——一个只让用户输入命令的 Shell 并无多少乐趣。然而,两个因素促使 Unix Shell 发生改变:用户体验和编程。现在的 Shell 已经不仅仅是接受命令这么简单了。

Modern shells are very convenient. For example, they remember commands that you’ve typed, and let you reuse those commands. Modern shells also let you edit those commands, so they don’t have to be the same each time. And modern shells let you define your own command abbreviations, shortcuts, and other features. For an experienced user, typing commands (e.g., with shorthand, shortcuts, and command completion) is a lot more efficient and effective than dragging things around in a fancy windowed interface.

现在的 Shell 非常方便。例如,它们会记住你输入过的命令,允许之后重用。现在的 Shell 也允许你编辑这些命令,所以这些命令不必每次都相同。现在的 Shell 允许你定义自己的命令缩写、快捷方式以及其它特性。对于有经验的用户来说,输入命令(例如使用速记、快捷方式、命令补充等)比在花哨的窗口界面中拖动东西效率更高。

But beyond simple convenience, shells are programmable. There are many sequences of commands that you type again and again. Whenever you do anything a second time, you should ask, “Can’t I write a program to do this for me?” You can. A shell is also a programming language that’s specially designed to work with your computer system’s commands. So, if you want to generate a thousand MP3 files from WAV files, you can write a shell program (or shell script). If you want to compress all of your system’s logfiles, you can write a shell script to do it. Whenever you find yourself doing a task repeatedly, you should try to automate it by writing a shell script. There are more powerful scripting languages, like Perl, Python, and Ruby, but the Unix shell (whatever flavor of shell you’re using) is a great place to start. After all, you already know how to type commands; why make things more complex?

但除了简单方便之外,Shell 还是可编程的。你可以反复键入一系列命令。每当你再次做某事儿时,你可能会问:“我能不能写程序为我做这些事?”你当然能。Shell 也是一种编程语言,专门用于处理计算机系统的命令。因此,如果你想根据 WAV 文件生成一千个 MP3 文件,你可以写一个 Shell 程序(或 Shell 脚本)来实现。如果你想压缩你的系统中的所有日志文件,你还可以写一个 Shell 脚本来实现。每当发现自己要反复执行一个任务时,你应当尝试编写脚本自动执行该任务。同样还有更强大的脚本语言,诸如 Perl、Python,以及 Ruby,但是 Unix Shell (或其他你正在使用的 Shell)是一个绝佳的起点。毕竟,你已经知道如何输入命令了,为什么要让事情变得更复杂呢?

文章

  • 1.1 为什么是 Bash?
  • 1.2 The bash Shell
  • 1.3 解码提示
  • 1.4 显示你的位置
  • 1.5 查找和运行命令
  • 1.6 获取有关文件的更多信息
  • 1.7 显示当前目录中的所有隐藏(点)文件
  • 1.8 使用 Shell 引用
  • 1.9 使用或替换内置命令和外部命令
  • 1.10 确定您是否以交互方式运行
  • 1.11 将 bash 设置为您的默认 Shell
  • 1.12 保持 bash 更新
  • 1.13 获取 Linux 的 bash
  • 1.14 获取 xBSD 的 bash
  • 1.15 获取 macOS 的 bash
  • 1.16 获取 unix 的 bash
  • 1.17 获取 Windows 的 bash
  • 1.18 不通过获取 bash 使用 bash
  • 1.19 了解有关 bash 文档的更多信息

发表评论

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据