1 自定义提示
默认的shell提示
[me@linuxbox ~]$
环境变量中的PS1(short for prompt string 1)定义了提示的格式
[me@linuxbox ~]$ echo $PS1
[\u@\h \W]\$
以下是一些常见的符号意义表 Table 13-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 | Hostname of the local machine minus the trailing domain name |
| \H | Full hostnam |
| \j | Number of jobs running in the current shell session. |
| \l | Name of the current terminal device. |
| \n | A newline characte. |
| \r | A carriag |
| \s | Name of the shell program |
| \t | Current time in 24-hour, hours:minutes:seconds format |
| \T | Current time in 12-hour forma |
| @ | Current time in 12-hour, AM/PM format |
| \A | Current time in 24-hour, hours:minutes format. |
| \u | Username of the current use |
| \v | Version number of the shel |
| \V | Version and release numbers of the shel |
| \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 during this shell session |
| $ | This displays a “$” character unless you have superuser privileges. In that case, it displays a “#” instead |
| [ | This signals the start of a series of one or more non-printing characters. It is used to embed non-printing control characters that manipulate the terminal emulator in some way, such as moving the cursor or changing text colors |
| ] | This signals the end of a non-printing character sequence. |
尝试更改shell提示 首先,备份PS1
[root@izuf67yuy6secatlp4sztez ~]# ps1_old="$PS1"
确认备份
[root@izuf67yuy6secatlp4sztez ~]# echo $ps1_old
[\u@\h \W]\$
可以使用以下命令来恢复PS1
[root@izuf67yuy6secatlp4sztez ~]# PS1="$ps1_old"
如果在等号后面为空字符串,命令行什么也不会显示
[root@izuf67yuy6secatlp4sztez ~]# PS1=
没有任何提示很不方便,因此可以替换空字符串为简约的提示,令PS1="\$ ",以下为一些尝试:
[root@izuf67yuy6secatlp4sztez exercise]# PS1="\$ "
$ PS1="\a\$ "
$ PS1="\A \h \$ "
09:45 izuf67yuy6secatlp4sztez $ PS1="<\u@\h \W>\$ "
<root@izuf67yuy6secatlp4sztez exercise>$ PS1="$ps1_old"
[root@izuf67yuy6secatlp4sztez exercise]#
2 增加颜色
大多数终端模拟器响应一些特定的非打印字符序列来控制字符属性(如 颜色,加粗,闪烁等)与光标位置。
在将显示的字符流中给终端发送ANSI转义码(American National Standards Institut escape code)可以控制字符颜色。ANSI转义码不会打印出来;终端将其看作指令来解释它们。在表13-1中可以看到,可以通过在\[与\]字符序列中嵌入非打印字符序列来完成指令。一个以八进制033开始(由ESC键产生)ANSI转义码,后面加上字符属性选项,再加上指令。比如,设置文本颜色为默认黑色指令为\033[0;30m 。
Table13-2: Escape Sequences Used to Set Text Colors
| SequenceText | Color |
|---|---|
| \033[0;30m | Black |
| \033[0;31m | Red |
| \033[0;32m | Green |
| \033[0;33m | Brown |
| \033[0;34m | Blue |
| \033[0;35m | Purple |
| \033[0;36m | Cyan |
| \033[0;37m | Light Gray |
| \033[1;30m | Dark Gray |
| \033[1;31m | Light Red |
| \033[1;32m | Light Green |
| \033[1;33m | Yellow |
| \033[1;34m | Light Blue |
| \033[1;35m | Light Purple |
| \033[1;36m | Light Cyan |
| \033[1;37m | White |
将提示字符颜色修改为红色:
[root@izuf67yuy6secatlp4sztez exercise]# PS1="\[\033[0;31m\]$PS1 "
[root@izuf67yuy6secatlp4sztez exercise]# ls
命令生效后,在提示后面输入的命令也为红色。为了修复这个问题,需要其它的转义码在提示后面告诉终端使用修改为红色之前的颜色。
[root@izuf67yuy6secatlp4sztez exercise]# PS1="\[\033[0;31m\]$PS1\[\033[0m\] "
[root@izuf67yuy6secatlp4sztez exercise]# ls
可以使用下表转义码来改变背景颜色: Table 13-3: Escape Sequences Used to Set Background Color
| Sequence | Background Colo |
|---|---|
| \033[0;40m | Black |
| \033[0;41m | Red |
| \033[0;42m | Green |
| \033[0;43m | Brown |
| \033[0;44m | Blue |
| \033[0;45m | Purple |
| \033[0;46m | Cyan |
| \033[0;47m | Ligh Gray |
例如,将提示背景颜色改为红色:
[root@izuf67yuy6secatlp4sztez exercise]# PS1="\[\033[0;41m\]$ps1_old\[\033[0m\] "
[root@izuf67yuy6secatlp4sztez exercise]#
3 移动光标
转义码可以用在光标位置。经常在屏幕上不同位置用来提供时钟或者一些其他信息,比如
Table 13-4: Cursor Movement Escape Sequences
| Escape | CodeAction |
|---|---|
| \033[l;cH | Move the cursor to line l and column c. |
| \033[nA | Move the cursor up n lines. |
| \033[nB | Move the cursor down n lines. |
| \033[nC | Move the cursor forward n characters. |
| \033[nD | Move the cursor backward n characters. |
| \033[2J | Clear the screen and move the cursor to the upper-left corner (line 0, column 0). |
| \033[K | Clear from the cursor position to the end of the current line. |
| \033[s | Store the current cursor position. |
| \033[u | Recall the stored cursor position |
使用上面转义符,可以构建提示,此提示在屏幕上方绘制红色的bar,里面包含每次提示显示的时间(使用黄色渲染):
PS1="\[\033[s\033[0;0H\033[0;41m\033[K\033[1;33m\t\033[0m\033[u\]<\u@\h \W>\$ "
下面是对上述命令各个部分命令的含义 Table 13-5: Breakdown of Complex Prompt String
| Sequence | Action |
|---|---|
| [ | Begins a non-printing character sequence. The real purpose of this is to allow bash to correctly calculate the size of the visible prompt. Without this, command line editing features will improperly position the cursor. |
| \033[s | Store the cursor position. This is needed to return to the prompt location after the bar and clock have been drawn at the top of the screen. Be aware that some terminal emulators do not honor this code. |
| \033[0;0H | Move the cursor to the upper-left corner, which is line 0, column 0. |
| \033[0;41m | Set the background color to red |
| \033[K | Clear from the current cursor location (the top-left corner) to the end of the line. Since the background color is now red, the line is cleared to that color, creating our bar. Note that clearing to the end of the line does not change the cursor position, which remains at the upper-left corner. |
| \033[1;33m | Set the text color to yellow |
| \t | Display the current time. While this is a “printing” element, we still include it in the non-printing portion of the prompt, because we don’t want bash to include the clock when calculating the true size of the displayed prompt. |
| \033[0m | Turn off color. This affects both the text and the background. |
| \033[u | Restore the cursor position saved earlier. |
| ] | End the non-printing characters sequence. |
| <\u@\h \W>$ | Prompt string |
执行结果:
11:01:10
[root@izuf67yuy6secatlp4sztez exercise]# PS1="\[\033[s\033[0;0H\033[0;41m\033[K\033[1;33m\t\033[0m\033[u\]$ps1_old"
[root@izuf67yuy6secatlp4sztez exercise]#
显然,每次输入这么长的命令的不明智的。可以把以下两条命令保存到 .bashrc 文件中:
PS1="\[\033[s\033[0;0H\033[0;41m\033[K\033[1;33m\t\033[0m\033[u\]<\u@\h \W>\$ "
export PS1