Section 30.1: 着色和自定义终端提示

作者就是这样设置自己的 PS1 变量的:

gitPS1(){
    gitps1=$(git branch 2>/dev/null | grep '*')
    gitps1="${gitps1:+ (${gitps1/#\* /})}"
    echo "$gitps1"
}
#Please use the below function if you are a mac user
gitPS1ForMac(){
        git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}
timeNow(){
        echo "$(date +%r)"
}
if [ "$color_prompt" = yes ]; then
    if [ x$EUID = x0 ]; then
        PS1='\[\033[1;38m\][$(timeNow)]\[\033[00m\]
        \[\033[1;31m\]\u\[\033[00m\]\[\033[1;37m\]@\[\033[00m\]\[\033[1;33m\]\h\[\033[00m\]
        \[\033[1;34m\]\w\[\033[00m\]\[\033[1;36m\]$(gitPS1)\[\033[00m\] \[\033[1;31m\]:/#\[\033[00m\] '
    else
        PS1='\[\033[1;38m\][$(timeNow)]\[\033[00m\]
        \[\033[1;32m\]\u\[\033[00m\]\[\033[1;37m\]@\[\033[00m\]\[\033[1;33m\]\h\[\033[00m\]
        \[\033[1;34m\]\w\[\033[00m\]\[\033[1;36m\]$(gitPS1)\[\033[00m\] \[\033[1;32m\]:/$\[\033[00m\] '
    fi
else
        PS1='[$(timeNow)] \u@\h \w$(gitPS1) :/$ '
fi

颜色参考:

# Colors
txtblk='\e[0;30m' # Black - Regular
txtred='\e[0;31m' # Red
txtgrn='\e[0;32m' # Green
txtylw='\e[0;33m' # Yellow
txtblu='\e[0;34m' # Blue
txtpur='\e[0;35m' # Purple
txtcyn='\e[0;36m' # Cyan
txtwht='\e[0;37m' # White
bldblk='\e[1;30m' # Black - Bold
bldred='\e[1;31m' # Red
bldgrn='\e[1;32m' # Green
bldylw='\e[1;33m' # Yellow
bldblu='\e[1;34m' # Blue
bldpur='\e[1;35m' # Purple
bldcyn='\e[1;36m' # Cyan
bldwht='\e[1;37m' # White
unkblk='\e[4;30m' # Black - Underline
undred='\e[4;31m' # Red
undgrn='\e[4;32m' # Green
undylw='\e[4;33m' # Yellow
undblu='\e[4;34m' # Blue
undpur='\e[4;35m' # Purple
undcyn='\e[4;36m' # Cyan
undwht='\e[4;37m' # White
bakblk='\e[40m' # Black - Background
bakred='\e[41m' # Red
badgrn='\e[42m' # Green
bakylw='\e[43m' # Yellow
bakblu='\e[44m' # Blue
bakpur='\e[45m' # Purple
bakcyn='\e[46m' # Cyan
bakwht='\e[47m' # White
txtrst='\e[0m' # Text Reset

注意: 在 ~/.bashrc 或 /etc/bashrc 或 ~/.bash_profile 或 ~./profile 文件(取决于操作系统)中进行更改并保存。

对于root,你可能还需要编辑/etc/bash.bashrc或/root/.bashrc文件

保存文件后,运行 source ~/.bashrc(针对特定发行版)。

注意:如果已将更改保存到 ~/.bashrc 中,请记得在 ~/.bash_profile 中添加 source ~/.bashrc,这样每次启动终端应用程序时 PS1 中的更改都会被记录下来。

Section 30.2: 在终端提示中显示 git 分支名称

你可以在 PS1 变量中使用函数,但要确保使用单引号或使用转义字符来表示特殊字符:

gitPS1(){
    gitps1=$(git branch 2>/dev/null | grep '*')
    gitps1="${gitps1:+ (${gitps1/#\* /})}"
    echo "$gitps1"
}

PS1='\u@\h:\w$(gitPS1)$ '

它会给出这样的提示

user@Host:/path (master)$

注意

在 ~/.bashrc 或 /etc/bashrc 或 ~/.bash_profile 或 ~./profile 文件(取决于操作系统)中进行更改并保存。

保存文件后运行 source ~/.bashrc(针对特定发行版)。

Section 30.3: 在终端提示符中显示时间

timeNow(){
        echo "$(date +%r)"
}

PS1='[$(timeNow)] \u@\h:\w$ '

它会给出这样的提示

[05:34:37 PM] user@Host:/path$

注意

在 ~/.bashrc 或 /etc/bashrc 或 ~/.bash_profile 或 ~./profile 文件(取决于操作系统)中进行更改并保存。

保存文件后,运行source ~/.bashrc(取决于发行版)。

Section 30.4: 使用 PROMPT_COMMAND 显示 git 分支

如果您在 git 仓库的文件夹中,最好能显示您当前所在的分支。在 ~/.bashrc 或 /etc/bashrc 中添加以下内容(需要使用 git):

function prompt_command {
    # Check if we are inside a git repository
    if git status > /dev/null 2>&1; then
            # Only get the name of the branch
            export GIT_STATUS=$(git status | grep 'On branch' | cut -b 10-)
    else
            export GIT_STATUS=""
    fi
}

# This function gets called every time PS1 is shown
PROMPT_COMMAND=prompt_command

PS1="\$GIT_STATUS \u@\h:\w\$ "

如果我们在 git 仓库内的文件夹中,则会输出这样的信息:

branch user@machine:~$

如果我们在一个普通文件夹内:

user@machine:~$

Section 30.5: 更改 PS1 提示符

要更改 PS1 提示,只需更改 PS1 shell 变量的值。该值可以设置在 ~/.bashrc 或 /etc/bashrc 文件中,具体取决于发行版。PS1 可以改为任何纯文本,如

PS1="hello "

除纯文本外,还支持许多反斜杠转义的特殊字符:

Format Action
\a 一个 ASCII Bell 字符(07)
\d 周月日期 “格式的日期(例如,”5 月 26 日(星期二)
\D{format} 格式将传递给 strftime(3),并将结果插入提示字符串中;如果格式为空,则将使用特定于本地的时间表示法。大括号是必需的
\e 一个 ASCII 转义字符 (033)
\h 显示主机名,直到第一个.
\H 主机名
\j 当前由 shell 管理的任务数
\l shell 的终端设备名称的基名
\n 换行符
\r 回车键
\s shell 的名称,即 $0 的基名(最后的斜线之后的部分)
\t 以 24 小时 HH:MM:SS 格式表示的当前时间
\T 以 12 小时 HH:MM:SS 格式表示的当前时间
\@ 12 小时上午/下午格式的当前时间
\A 24 小时 HH:MM 格式的当前时间
\u 当前用户的用户名
\v bash 的版本(如 2.00)
\V bash 的版本、版本号 + 补丁级别(如 2.00.0)
\w 当前工作目录,$HOME 用破折号缩写
\W 当前工作目录的基名,$HOME 用破折号缩写
\! 该命令的历史编号
\# 该命令的命令号
\$ 如果有效 UID 为 0,则为 #,否则为 $
\nnn* 与八进制数 nnnn 相对应的字符
\ a 反斜杠
\[ 开始一串非打印字符,可用于在提示符中嵌入终端控制序列
\] 结束非打印字符序列

例如,我们可以将 PS1 设置为

PS1="\u@\h:\w\$ "

它就会输出:

cuser@machine:~$

Section 30.6: 显示上一条命令的返回状态和时间

有时,我们需要一个直观的提示来显示上一条命令的返回状态。下面的代码段将其放在 PS1 的头部。

请注意,每次生成新的 PS1 时都应调用 __stat()函数,否则它就会一直显示你的 .bashrc 或 .bash_profile 中最后一条命令的返回状态。

# -ANSI-COLOR-CODES- #
Color_Off="\033[0m"
###-Regular-###
Red="\033[0;31m"
Green="\033[0;32m"
Yellow="\033[0;33m"
####-Bold-####

function __stat() {
    if [ $? -eq 0 ]; then
            echo -en "$Green ✔ $Color_Off "
    else
            echo -en "$Red ✘ $Color_Off "
    fi
}

PS1='$(__stat)'
PS1+="[\t] "
PS1+="\e[0;33m\u@\h\e[0m:\e[1;34m\w\e[0m \n$ "

export PS1