bash中PS1的设置

目的

装逼

过程

通过man

1
$ man bash

打开bash的帮助页后, 通过 /PS1 找到具体的可配置项.

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
32
33
PROMPTING
When executing interactively, bash displays the primary prompt PS1 when it is ready to read a command, and the secondary prompt PS2 when it needs more input to complete a command.
Bash allows these prompt strings to be customized by inserting a number of backslash-escaped special characters that are decoded as follows:
\a an ASCII bell character (07)
\d the date in "Weekday Month Date" format (e.g., "Tue May 26")
\D{format}
the format is passed to strftime(3) and the result is inserted into the prompt string; an empty format results in a locale-specific time representation. The braces
are required
\e an ASCII escape character (033)
\h the hostname up to the first `.'(主机名,非完整的)
\H the hostname(完整的主机名)
\j the number of jobs currently managed by the shell
\l the basename of the shell's terminal device name
\n newline(换行)
\r carriage return
\s the name of the shell, the basename of $0 (the portion following the final slash)
\t the current time in 24-hour HH:MM:SS format
\T the current time in 12-hour HH:MM:SS format
\@ the current time in 12-hour am/pm format
\A the current time in 24-hour HH:MM format<和上面三个都是指示时间的>
\u the username of the current user(用户名)
\v the version of bash (e.g., 2.00)
\V the release of bash, version + patch level (e.g., 2.00.0)
\w the current working directory, with $HOME abbreviated with a tilde (uses the value of the PROMPT_DIRTRIM variable)(完整的工作目录)
\W the basename of the current working directory, with $HOME abbreviated with a
tilde(只会列出最后一个目录)
\! the history number of this command
\# the command number of this command
\$ if the effective UID is 0, a #, otherwise a $(提示符)
\nnn the character corresponding to the octal number nnn
\\ a backslash
\[ begin a sequence of non-printing characters, which could be used to embed a terminal control sequence into the prompt
\] end a sequence of non-printing characters

颜色配置

可以通过设置PS1可以使提示符为彩色, 设置的格式为: \[\e[F;Bm\], F为字体颜色, B为背景色, 颜色编号如下表格; 通过\[\e[0m\]关闭颜色输出; 当B为1时,将显示加亮加粗的文字.

前景 背景 颜色
30 40 黑色
31 41 红色
32 42 绿色
33 43 黄色
34 44 蓝色
35 45 紫红色
36 46 青蓝色
37 47 白色
代码 意义
0 OFF
1 高亮显示
4 underline
5 闪烁
7 反白显示
8 不可见

例子

1
PS1="[\[\e[32;1m\]\u\[\e[m\]\[\e[1;36m\]@\[\e[m\]\[\e[1;35m\]\A\[\e[m\]]->[\[\e[1;33m\]\w\[\e[m\]]\$ "

将颜色代码去掉, 剩下的为: PS1="[\u@\A]->[\w]\$", \u代表用户名, @代表本身,\A代表一个HH:MM格式的时间, \w代表一个完整的路径,\$代表提示符, 其他的就是时间.

其他

自己慢慢测吧!

Bash 里面提示 git状态

1
2
3
4
$ cd /usr/share/git/completion
$ cp git-completion.bash ~/.oh-my-bash/git-completion.bash
$ cp git-prompt.sh ~/.oh-my-bash/git-prompt.sh
$ cd ~

编辑.bashrc, 添加以下内容:

1
2
3
4
5
source ~/.oh-my-bash/git-completion.bash
source ~/.oh-my-bash/git-prompt.sh

# PS1修改为如下:
PS1='[\[\e[1;32m\]\u\[\e[m\]\[\e[1;36m\]@\[\e[m\]\[\e[1;35m\]\A\[\e[m\]]->[\[\e[1;33m\]\w\[\e[m\]\[\e[1;32m\]$(__git_ps1 " (%s)")\[\e[m\]]\$ '

具体的使用步骤在git-completion.bashgit-prompt.sh的开始处找到.