linux 命令行显示优化

转载TLCL

目录
自定制 shell 提示符

In this chapter we will look at a seemingly trivial detail — our shell prompt. This examination will reveal some of the inner workings of the shell and the terminal emulator program itself.

在这一章中,我们将会看一下表面上看来很琐碎的细节-shell 提示符。但这会揭示一些 shell 和 终端仿真器的内部工作方式。

Like so many things in Linux, the shell prompt is highly configurable, and while we have pretty much taken it for granted, the prompt is a really useful device once we learn how to control it.

和 Linux 内的许多程序一样,shell 提示符是可高度配置的,虽然我们把它相当多地看作是理所当然的, 但是我们一旦学会了怎样控制它,shell 提示符是一个相当有用的工具。

Anatomy Of A Prompt

解剖一个提示符
Our default prompt looks something like this:

我们默认的提示符看起来像这样:

[me@linuxbox ~]$
Notice that it contains our user name, our host name and our current working directory, but how did it get that way? Very simply, it turns out. The prompt is defined by an environment variable named PS1 (short for “prompt string one”). We can view the contents of PS1 with the echo command:

注意它包含我们的用户名,主机名和当前工作目录,但是它又是怎样得到这些东西的呢? 结果证明非常简单。提示符是由一个环境变量定义的,叫做 PS1(是“prompt string one” 的简写)。我们可以通过 echo 命令来查看 PS1的内容。

[me@linuxbox ~] echo e c h o PS1
[\u@\h \W]$
Note: Don’t worry if your results are not exactly the same as the example above. Every Linux distribution defines the prompt string a little differently, some quite exotically.

注意:如果你 shell 提示符的内容和上例不是一模一样,也不必担心。每个 Linux 发行版 定义的提示符稍微有点不同,其中一些相当异于寻常。

From the results, we can see that PS1 contains a few of the characters we see in our prompt such as the brackets, the at-sign, and the dollar sign, but the rest are a mystery. The astute among us will recognize these as backslash-escaped special characters like those we saw in Chapter 8. Here is a partial list of the characters that the shell treats specially in the prompt string:

从输出结果中,我们看到那个 PS1 环境变量包含一些这样的字符,比方说中括号,@符号,和美元符号, 但是剩余部分就是个谜。我们中一些机敏的人会把这些看作是由反斜杠转义的特殊字符,就像我们 在第八章中看到的一样。这里是一部分字符列表,在提示符中 shell 会特殊对待这些字符:

Table 14-1: Escape Codes Used In Shell Prompts
Sequence Value Displayed
\a ASCII bell. This makes the computer beep when it is encountered.
\d Current date in day, month, date format. For example, “Mon May 26.”
\h Host name of the local machine minus the trailing domain name.
\H Full host name.
\j Number of jobs running in the current shell session.
\l Name of the current terminal device.
\n A newline character.
\r A carriage return.
\s Name of the shell program.
\t Current time in 24 hour hours:minutes:seconds format.
\T Current time in 12 hour format.
\@ Current time in 12 hour AM/PM format.
\A Current time in 24 hour hours:minutes format.
\u User name of the current user.
\v Version number of the shell.
\V Version and release numbers of the shell.
\w Name of the current working directory.
\W Last part of the current working directory name.
! History number of the current command.
# Number of commands entered into this shell session.
$ This displays a “” character unless you have superuser privileges. In that case, it displays a “#” instead.  
[  Signals the start of a series of one or more non-printing characters. This is used to embed non-printing control characters which manipulate the terminal emulator in some way, such as moving the cursor or changing text colors.  
]  Signals the end of a non-printing character sequence.  
表14-1: Shell 提示符中用到的转义字符  
序列  显示值  
\a  以 ASCII 格式编码的铃声 . 当遇到这个转义序列时,计算机会发出嗡嗡的响声。  
\d  以日,月,天格式来表示当前日期。例如,“Mon May 26.”  
\h  本地机的主机名,但不带末尾的域名。  
\H  完整的主机名。  
\j  运行在当前 shell 会话中的工作数。  
\l  当前终端设备名。  
\n  一个换行符。  
\r  一个回车符。  
\s  shell 程序名。  
\t  以24小时制,hours:minutes:seconds 的格式表示当前时间.  
\T  以12小时制表示当前时间。  
\@  以12小时制,AM/PM 格式来表示当前时间。  
\A  以24小时制,hours:minutes 格式表示当前时间。  
\u  当前用户名。  
\v  shell 程序的版本号。  
\V  Version and release numbers of the shell.  
\w  当前工作目录名。  
\W  当前工作目录名的最后部分。  
!  当前命令的历史号。  
#  当前 shell 会话中的命令数。  
\$ 这会显示一个”
” character unless you have superuser privileges. In that case, it displays a “#” instead.  [  Signals the start of a series of one or more non-printing characters. This is used to embed non-printing control characters which manipulate the terminal emulator in some way, such as moving the cursor or changing text colors.  ]  Signals the end of a non-printing character sequence.  表14-1: Shell 提示符中用到的转义字符  序列  显示值  \a  以 ASCII 格式编码的铃声 . 当遇到这个转义序列时,计算机会发出嗡嗡的响声。  \d  以日,月,天格式来表示当前日期。例如,“Mon May 26.”  \h  本地机的主机名,但不带末尾的域名。  \H  完整的主机名。  \j  运行在当前 shell 会话中的工作数。  \l  当前终端设备名。  \n  一个换行符。  \r  一个回车符。  \s  shell 程序名。  \t  以24小时制,hours:minutes:seconds 的格式表示当前时间.  \T  以12小时制表示当前时间。  \@  以12小时制,AM/PM 格式来表示当前时间。  \A  以24小时制,hours:minutes 格式表示当前时间。  \u  当前用户名。  \v  shell 程序的版本号。  \V  Version and release numbers of the shell.  \w  当前工作目录名。  \W  当前工作目录名的最后部分。  !  当前命令的历史号。  #  当前 shell 会话中的命令数。  \$ 这会显示一个”
”字符,除非你拥有超级用户权限。在那种情况下, 它会显示一个”#”字符。
[ 标志着一系列一个或多个非打印字符的开始。这被用来嵌入非打印 的控制字符,这些字符以某种方式来操作终端仿真器,比方说移动光标或者是更改文本颜色。
] 标志着非打印字符序列结束。
Trying Some Alternate Prompt Designs

试试一些可替代的提示符设计
With this list of special characters, we can change the prompt to see the effect. First, we’ll back up the existing string so we can restore it later. To do this, we will copy the existing string into another shell variable that we create ourselves:

参照这个特殊字符列表,我们可以更改提示符来看一下效果。首先, 我们把原来提示符字符串的内容备份一下,以备之后恢复原貌。为了完成备份, 我们把已有的字符串复制到另一个 shell 变量中,这个变量是我们自己创造的。

[me@linuxbox ~] ps1old=" p s 1 o l d =" PS1”
We create a new variable called ps1_old and assign the value of PS1 to it. We can verify that the string has been copied with the echo command:

我们新创建了一个叫做 ps1_old 的变量,并把变量 PS1的值赋 ps1_old。通过 echo 命令可以证明 我们的确复制了 PS1的值。

[me@linuxbox ~] echo e c h o ps1_old
[\u@\h \W]$
We can restore the original prompt at any time during our terminal session by simply reversing the process:

在终端会话中,我们能在任一时间复原提示符,只要简单地反向操作就可以了。

[me@linuxbox ~] PS1=" P S 1 =" ps1_old”
Now that we are ready to proceed, let’s see what happens if we have an empty prompt string:

现在,我们准备开始,让我们看看如果有一个空的字符串会发生什么:

[me@linuxbox ~]$ PS1=
If we assign nothing to the prompt string, we get nothing. No prompt string at all! The prompt is still there, but displays nothing, just as we asked it to. Since this is kind of disconcerting to look at, we’ll replace it with a minimal prompt:

如果我们没有给提示字符串赋值,那么我们什么也得不到。根本没有提示字符串!提示符仍然在那里, 但是什么也不显示,正如我们所要求的那样。我们将用一个最小的提示符来代替它:

PS1=”$ ”
That’s better. At least now we can see what we are doing. Notice the trailing space within the double quotes. This provides the space between the dollar sign and the cursor when the prompt is displayed.

这样要好一些。至少能看到我们在做什么。注意双引号中末尾的空格。当提示符显示的时候, 这个空格把美元符号和光标分离开。

Let’s add a bell to our prompt:

在提示符中添加一个响铃:

$ PS1=”\a\$ ”
Now we should hear a beep each time the prompt is displayed. This could get annoying, but it might be useful if we needed notification when an especially long-running command has been executed.

现在每次提示符显示的时候,我们应该能听到嗡嗡声。这会变得很烦人,但是它可能会 很有用,特别是当一个需要运行很长时间的命令执行完后,我们要得到通知。

Next, let’s try to make an informative prompt with some host name and time-of-day information:

下一步,让我们试着创建一个信息丰富的提示符,包含主机名和当天时间的信息。

PS1=\A\h$17:33linuxbox P S 1 = ” \A \h $ ” 17 : 33 l i n u x b o x
Try out the other sequences listed in the table above and see if you can come up with a brilliant new prompt.

试试其他上表中列出的转义序列,看看你能否想出精彩的新提示符。

Adding Color

添加颜色
Most terminal emulator programs respond to certain non-printing character sequences to control such things as character attributes (like color, bold text and the dreaded blinking text) and cursor position. We’ll cover cursor position in a little bit, but first we’ll look at color.

大多数终端仿真器程序支持一定的非打印字符序列来控制,比方说字符属性(像颜色,黑体和可怕的闪烁) 和光标位置。我们会更深入地讨论光标位置,但首先我们要看一下字体颜色。

Terminal Confusion

混乱的终端时代

Back in ancient times, when terminals were hooked to remote computers, there were many competing brands of terminals and they all worked differently. They had different keyboards and they all had different ways of interpreting control information. Unix and Unix-like systems have two rather complex subsystems to deal with the babel of terminal control (called termcap and terminfo). If you look in the deepest recesses of your terminal emulator settings you may find a setting for the type of terminal emulation.

回溯到终端连接到远端计算机的时代,有许多竞争的终端品牌,它们各自工作不同。 它们有着不同的键盘,以不同的方式来解释控制信息。Unix 和类 Unix 的系统有两个 相当复杂的子系统来处理终端控制领域的混乱局面(称为 termcap 和 terminfo)。如果你 查看一下终端仿真器最底层的属性设置,可能会找到一个关于终端仿真器类型的设置。

In an effort to make terminals speak some sort of common language, the American National Standards Institute (ANSI) developed a standard set of character sequences to control video terminals. Old time DOS users will remember the ANSI.SYS file that was used to enable interpretation of these codes.

为了努力使所有的终端都讲某种通用语言,美国国家标准委员会(ANSI)制定了 一套标准的字符序列集合来控制视频终端。原先 DOS 用户会记得 ANSI.SYS 文件, 这是一个用来使这些编码解释生效的文件。

Character color is controlled by sending the terminal emulator an ANSI escape code embedded in the stream of characters to be displayed. The control code does not “print out” on the display, rather it is interpreted by the terminal as an instruction. As we saw in the table above, the [ and ] sequences are used to encapsulate non-printing characters. An ANSI escape code begins with an octal 033 (the code generated by the escape key) followed by an optional character attribute followed by an instruction. For example, the code to set the text color to normal (attribute = 0), black text is:

字符颜色是由发送到终端仿真器的一个嵌入到了要显示的字符流中的 ANSI 转义编码来控制的。 这个控制编码不会“打印”到屏幕上,而是被终端解释为一个指令。正如我们在上表看到的字符序列, 这个 [ 和 ] 序列被用来封装这些非打印字符。一个 ANSI 转义编码以一个八进制033(这个编码是由 退出按键产生的)开头,其后跟着一个可选的字符属性,在之后是一个指令。例如,把文本颜色 设为正常(attribute = 0),黑色文本的编码如下:

\033[0;30m
Here is a table of available text colors. Notice that the colors are divided into two groups, differentiated by the application of the bold character attribute (1) which creates the appearance of “light” colors:

这里是一个可用的文本颜色列表。注意这些颜色被分为两组,由应用程序粗体字符属性(1) 分化开来,这个属性可以描绘出“浅”色文本。

Table 14-2: Escape Sequences Used To Set Text Colors
Sequence Text Color Sequence Text Color
\033[0;30m Black \033[1;30m Dark Gray
\033[0;31m Red \033[1;31m Light Red
\033[0;32m Green \033[1;32m Light Green
\033[0;33m Brown \033[1;33m Yellow
\033[0;34m Blue \033[1;34m Light Blue
\033[0;35m Purple \033[1;35m Light Purple
\033[0;36m Cyan \033[1;36m Light Cyan
\033[0;37m Light Gray \033[1;37m White
表14-2: 用转义序列来设置文本颜色
序列 文本颜色 序列 文本颜色
\033[0;30m 黑色 \033[1;30m 深灰色
\033[0;31m 红色 \033[1;31m 浅红色
\033[0;32m 绿色 \033[1;32m 浅绿色
\033[0;33m 棕色 \033[1;33m 黄色
\033[0;34m 蓝色 \033[1;34m 浅蓝色
\033[0;35m 粉红 \033[1;35m 浅粉色
\033[0;36m 青色 \033[1;36m 浅青色
\033[0;37m 浅灰色 \033[1;37m 白色
Let’s try to make a red prompt. We’ll insert the escape code at the beginning:

让我们试着制作一个红色提示符。我们将在开头加入转义编码:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值