## # 什么是 shell

shell is a program that takes keyboard commands and passes them to the
operating system to carry out. Almost all Linux distributions supply a shell
program from the GNU Project called bash. The name “bash” is an acronym for
“Bourne Again SHell”, a reference to the fact bash is an enhanced replacement
for sh, the original Unix shell program written by Steve Bourne.

### # 终端仿真器

When using a graphical user interface, we need another program called a
terminal emulator to interact with the shell. If we look through our desktop
menus, we will probably find one. KDE uses konsole and GNOME uses
gnome-terminal, though it's likely called simply “terminal” on our menu. There
are a number of other terminal emulators available for Linux, but they all
basically do the same thing; give us access to the shell. You will probably
develop a preference for one or another based on the number of bells and
whistles it has.

### # 第一次按键

So let's get started. Launch the terminal emulator! Once it comes up, we should see somehing like this:

[me@linuxbox ~]$ 1 This is called a shell prompt and it will appear whenever the shell is ready to accept input. While it may vary in appearance somewhat depending on the distribution, it will usually include your username@machinename, followed by the current working directory (more about that in a little bit) and a dollar sign. 这叫做 shell 提示符，无论何时当 shell 准备好了去接受输入时，它就会出现。然而， 它可能会以各种各样的面孔显示，这则取决于不同的 Linux 发行版， 它通常包括你的用户名@主机名，紧接着当前工作目录（稍后会有更多介绍）和一个美元符号。 If the last character of the prompt is a pound sign (“#”) rather than a dollar sign, the terminal session has superuser privileges. This means either we are logged in as the root user or we selected a terminal emulator that provides superuser (administrative) privileges. 如果提示符的最后一个字符是“#”, 而不是“$”, 那么这个终端会话就有超级用户权限。

Assuming that things are good so far, let's try some typing. Type some
gibberish at the prompt like so:

[me@linuxbox ~]$kaekfjaeifj  1 Since this command makes no sense, the shell will tell us so and give us another chance: 因为这个命令没有任何意义，所以 shell 会提示错误信息，并让我们再试一下： bash: kaekfjaeifj: command not found [me@linuxbox ~]$

1
2

### # 命令历史

If we press the up-arrow key, we will see that the previous command “kaekfjaeifj”
reappears after the prompt. This is called command history. Most Linux distributions
remember the last five hundred commands by default. Press the down-arrow key and the
previous command disappears.

### # 移动光标

Recall the previous command with the up-arrow key again. Now try the left and right-arrow keys.
See how we can position the cursor anywhere on the command line? This makes editing commands easy.

### # 关于鼠标和光标

While the shell is all about the keyboard, you can also use a mouse with your
terminal emulator. There is a mechanism built into the X Window System (the
underlying engine that makes the GUI go) that supports a quick copy and paste
technique. If you highlight some text by holding down the left mouse button and
dragging the mouse over it (or double clicking on a word), it is copied into a
buffer maintained by X. Pressing the middle mouse button will cause the text to
be pasted at the cursor location. Try it.

（使 GUI 工作的底层引擎）内建了一种机制，支持快速拷贝和粘贴技巧。

Note: Don't be tempted to use Ctrl-c and Ctrl-v to perform copy and paste
inside a terminal window. They don't work. These control codes have different
meanings to the shell and were assigned many years before Microsoft Windows.

Microsoft Windows （定义复制粘贴的含义）许多年之前就赋予了不同的意义。

Your graphical desktop environment (most likely KDE or GNOME), in an effort
to behave like Windows, probably has its focus policy set to “click to focus.”
This means for a window to get focus (become active) you need to click on it.
This is contrary to the traditional X behavior of “focus follows mouse” which
means that a window gets focus by just passing the mouse over it. The window
will not come to the foreground until you click on it but it will be able to receive
input. Setting the focus policy to “focus follows mouse” will make the copy and
paste technique even more useful. Give it a try. I think if you give it a chance
you will prefer it. You will find this setting in the configuration program for your
window manager.

### # 试试运行一些简单命令

Now that we have learned to type, let's try a few simple commands. The first one is
date. This command displays the current time and date.

[me@linuxbox ~]$date Thu Oct 25 13:51:54 EDT 2007  1 2 A related command is cal which, by default, displays a calendar of the current month. 一个相关联的命令，cal，它默认显示当前月份的日历。 [me@linuxbox ~]$ cal
October 2007
Su Mo Tu We Th Fr Sa
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31

1
2
3
4
5
6
7
8

To see the current amount of free space on your disk drives, type df:

[me@linuxbox ~]$df Filesystem 1K-blocks Used Available Use% Mounted on /dev/sda2 15115452 5012392 9949716 34% / /dev/sda5 59631908 26545424 30008432 47% /home /dev/sda1 147764 17370 122765 13% /boot tmpfs 256856 0 256856 0% /dev/shm  1 2 3 4 5 6 Likewise, to display the amount of free memory, type the free command. 同样地，显示空闲内存的数量，输入命令 free。 [me@linuxbox ~]$ free
total       used       free     shared    buffers     cached
Mem:       2059676     846456    1213220          0
44028      360568
-/+ buffers/cache:     441860    1617816
Swap:      1042428          0    1042428

1
2
3
4
5
6

### # 结束终端会话

We can end a terminal session by either closing the terminal emulator window, or by
entering the exit command at the shell prompt:

[me@linuxbox ~]\$ exit

1

### # 幕后控制台

Even if we have no terminal emulator running, several terminal sessions
continue to run behind the graphical desktop. Called virtual terminals or
virtual consoles, these sessions can be accessed on most Linux distributions
by pressing Ctrl- Alt-F1 through Ctrl-Alt-F6 on most systems. When a session
is accessed, it presents a login prompt into which we can enter our user name
and password. To switch from one virtual console to another, press Alt and