[转]Bash中的PS1详解
10.9日某公司电面提到的bash题目。
“bash里面的这个提示符前的内容是否可以修改?如果可以,应该是怎么改(不需要确切说出修改那里)”
当时回答不太确切,只是说这个都可以在配置文件里面改,显示顺序和显示内容都可以修改。
面试虽然完了,但是面试的问题必须要解决。所以,现在来弄清楚。
PS1: pormot symbol 提示字元
先看看操作系统版本
[jim@localhost /]$ lsb_release-a
LSB Version::core-3.1-ia32:core-3.1-noarch:core-3.2-ia32:core-3.2-noarch:desktop-3.1-ia32:desktop-3.1-noarch:desktop-3.2-ia32:desktop-3.2-noarch
Distributor ID:Fedora
Description:Fedora release 10 (Cambridge)
Release:10
Codename:Cambridge
[jim@localhost /]$
接下来看看bash中的PS1格式:
[jim@localhost /]$ set | grep PS1
PS1='[\u@\h \W]\$ '
[jim@localhost /]$
PS1是bash环境变量预设的值,各个参数的说明如下:
PROMPTING
When executing interactively, bash displays the primary prompt PS1 when
itisreadytoread a command, and the secondary prompt PS2 when it
needs more input to completeacommand.Bashallowstheseprompt
stringstobecustomizedby inserting a number of backslash-escaped
special characters that are decoded as follows:
\aan ASCII bell character (07)
\dthe date in "Weekday Month Date" format (e.g.,"TueMay
26")
\D{format}
theformatispassedto strftime(3) and the result is
inserted into the prompt string;
an empty formatresults
in a locale-specific time representation.The braces are
required
\ean ASCII escape character (033)
\hthe hostname up to the first ‘.’
\Hthe hostname
\jthe number of jobs currently managed by the shell
\lthe basename of the shell’s terminal device name
\nnewline
\rcarriage return
\sthe name of the shell, the basename of$0(theportion
following the final slash)
\tthe current time in 24-hour HH:MM:SS format
\Tthe current time in 12-hour HH:MM:SS format
\@the current time in 12-hour am/pm format
\Athe current time in 24-hour HH:MM format
\uthe username of the current user
\vthe version of bash (e.g., 2.00)
\Vthe release of bash, version + patch level (e.g., 2.00.0)
\wthe current workingdirectory,with$HOMEabbreviated
with a tilde
\Wthe 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 $
\nnnthe character corresponding to the octal number nnn
\\a backslash
\[begin a sequence of non-printing characters, whichcould
beusedtoembeda terminal control sequence into the
prompt
\]end a sequence of non-printing characters
如果想把自己的PS1 修改为下面这样:
[jmi@localhost 00:21 #2]$
#2表示是在该bash中的第二次command
该怎么修改呢?
PS1='[\u@\h \A#\#]\$'
一般路径会在shell的顶端显示,所以路径信息不用显示在PS中。
localhost这种域名一般也用不到,也可以删掉。
其实我觉得嘛,所有的信息都可以去掉。因为root和普通用户也只是$和~的区别。
不过用户名还是需要的,可以作为区分。
所以最后的PS应该是:
PS1='[\u]\$'
推荐阅读
- 热闹中的孤独
- Shell-Bash变量与运算符
- JS中的各种宽高度定义及其应用
- 我眼中的佛系经纪人
- 《魔法科高中的劣等生》第26卷(Invasion篇)发售
- Android中的AES加密-下
- 流转
- 放下心中的偶像包袱吧
- 一起来学习C语言的字符串转换函数
- C语言字符函数中的isalnum()和iscntrl()你都知道吗