[转]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
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
\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 修改为下面这样:
[jmi@localhost 00:21 #2]$
#2表示是在该bash中的第二次command
该怎么修改呢?
PS1='[\u@\h \A #\#]\$'
一般路径会在shell的顶端显示,所以路径信息不用显示在PS中。
localhost这种域名一般也用不到,也可以删掉。
其实我觉得嘛,所有的信息都可以去掉。因为root和普通用户也只是$和~的区别。
不过用户名还是需要的,可以作为区分。
所以最后的PS应该是:
PS1='[\u]\$'
“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
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
\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 修改为下面这样:
[jmi@localhost 00:21 #2]$
#2表示是在该bash中的第二次command
该怎么修改呢?
PS1='[\u@\h \A #\#]\$'
一般路径会在shell的顶端显示,所以路径信息不用显示在PS中。
localhost这种域名一般也用不到,也可以删掉。
其实我觉得嘛,所有的信息都可以去掉。因为root和普通用户也只是$和~的区别。
不过用户名还是需要的,可以作为区分。
所以最后的PS应该是:
PS1='[\u]\$'