| Escape | Details |
|---|---|
\a |
一个响铃字符 |
\d |
日期,采用 “周月日期 ”格式(例如,“Tue May 26”)。 |
\D{FORMAT} |
FORMAT 将传递给 `strftime'(3),结果将插入提示字符串;空 FORMAT 将导致特定于本地的时间表示。大括号是必需的。 |
\e |
escape 字符。\033 当然也可以。 |
\h |
主机名,直至第一个“.”。(即没有域名部分) |
\H |
主机名最终包含域名部分 |
\j |
shell 当前管理的工作数量。 |
\l |
shell 的终端设备名称的基名。 |
\n |
换行。 |
\r |
回车。 |
\s |
shell 的名称,即 `$0'(最后一个斜线之后的部分)的基名。 |
\t |
时间,24 小时 HH:MM:SS 格式。 |
\T |
时间,采用 12 小时 HH:MM:SS 格式。 |
@ |
时间,以 12 小时 am/pm 格式表示。 |
\A |
时间,24 小时 HH:MM 格式。 |
\u |
当前用户的用户名。 |
\v |
Bash 的版本(例如 2.00) |
\V |
Bash 的版本、版本号 + 补丁级别(例如,2.00.0) |
\w |
当前工作目录,$HOME 用斜线缩写(使用 $PROMPT_DIRTRIM 变量)。 |
\W |
$PWD 的基名,其中 $HOME 用斜线缩写。 |
! |
该命令的历史编号。 |
# |
该命令的命令编号。 |
$ |
如果有效 uid 为 0,则 #,否则 $。 |
\NNN |
ASCII 码为八进制数值 NNNN 的字符。 |
\ |
反斜线 |
\[ |
开始一个非打印字符序列。这可用于在提示符中嵌入终端控制序列。 |
\] |
结束一个非打印字符序列。 |
Section 54.1: 使用 PROMPT_COMMAND 环境变量
当交互式 bash 实例中的最后一条命令执行完毕时,将显示已评估的 PS1 变量。在实际显示 PS1 之前,bash 会查看是否设置了 PROMPT_COMMAND。该变量的值必须是一个可调用的程序或脚本。如果设置了该变量,则会在显示 PS1 提示之前调用该程序/脚本。
# just a stupid function, we will use to demonstrate
# we check the date if Hour is 12 and Minute is lower than 59
lunchbreak(){
if (( $(date +%H) == 12 && $(date +%M) < 59 )); then
# and print colored \033[ starts the escape sequence
# 5; is blinking attribute
# 2; means bold
# 31 says red
printf "\033[5;1;31mmind the lunch break\033[0m\n";
else
printf "\033[33mstill working...\033[0m\n";
fi;
}
# activating it
export PROMPT_COMMAND=lunchbreak
Section 54.2: 使用 PS2
当命令超过一行,而 bash 等待更多按键时,PS2 就会显示。当输入复合命令,如 while...do...done 和 alike 时,也会显示 PS2。
export PS2="would you please complete this command?\n"
# now enter a command extending to at least two lines to see PS2
Section 54.3: 使用 PS3
执行选择语句时,会显示以数字为前缀的给定项目,然后显示 PS3 提示:
export PS3=" To choose your language type the preceding number : "
select lang in EN CA FR DE; do
# check input here until valid.
break
done
Section 54.4: 使用 PS4
当 bash 处于调试模式时,会显示 PS4。
#!/usr/bin/env bash
# switch on debugging
set -x
# define a stupid_func
stupid_func(){
echo I am line 1 of stupid_func
echo I am line 2 of stupid_func
}
\# setting the PS4 "DEBUG" prompt
export PS4='\nDEBUG level:$SHLVL subshell-level: $BASH_SUBSHELL \nsource-file:${BASH_SOURCE}
line#:${LINENO} function:${FUNCNAME[0]:+${FUNCNAME[0]}(): }\nstatement: '
\# a normal statement
echo something
\# function call
stupid_func
\# a pipeline of commands running in a subshell
( ls -l | grep 'x' )
Section 54.5: 使用 PS1
PS1 是正常的系统提示符,表示 bash 等待输入命令。它能理解一些转义序列,并能执行函数或程序。由于 bash 必须在显示提示符后定位光标,因此它需要知道如何计算提示字符串的有效长度。为了表示 PS1 变量中的非打印字符序列,使用了转义大括号: [ a non printing sequence of chars ]. 以上所述适用于所有 PS* 变量。
(黑色圆点表示光标)
#everything not being an escape sequence will be literally printed
export PS1="literal sequence " # Prompt is now:
literal sequence ▉
# \u == user \h == host \w == actual working directory
# mind the single quotes avoiding interpretation by shell
export PS1='\u@\h:\w > ' # \u == user, \h == host, \w actual working dir
looser@host:/some/path > ▉
# executing some commands within PS1
# following line will set foreground color to red, if user==root,
# else it resets attributes to default
# $( (($EUID == 0)) && tput setaf 1)
# later we do reset attributes to default with
# $( tput sgr0 )
# assuming being root:
PS1="\[$( (($EUID == 0)) && tput setaf 1 \]\u\[$(tput sgr0)\]@\w:\w \$ "
looser@host:/some/path > ▉ # if not root else <red>root<default>@host....