类 UNIX系统里的PS1 (以MAC为例)

2420阅读 0评论2019-11-18 starb6
分类:LINUX


关于PS1:
即登陆提示符,可以显示一些类似机器名,用户,时间等等信息,内容与颜色和格式,都是可以设定的,设定方式为:PS1=设定值。

PS1 (Prompt String 1) is one of the prompts available in Linux/Unix. When you try to login to any machine, you have to enter user name and password. Once you are done with this you are presented with some info like who logged in, on what machine he logged in, what is his present working directory and if the logged in user is a super user or a normal user.


查看:

/etc/bashrc

MCdeMacBook-Pro:mylib mc$ cat /etc/bashrc

查看:

MCdeMacBook-Pro:mylib mc$ echo $PS1

\h:\W \u\$

参数意义:

d - the date in "Weekday Month Date" format (e.g., "Tue May 26”)

\d :代表日期,格式为weekday month date,例如:"Mon Aug 1" 

e - an ASCII escape character (033)

h - the hostname up to the first .

\h :仅取主机的第一个名字

H - the full hostname

\H :完整的主机名称。

j - the number of jobs currently run in background

l - the basename of the shells 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 :显示时间为24小时格式,如:HHMMSS

T - the current time in 12-hour HH:MM:SS format

\T :显示时间为12小时格式

@ - the current time in 12-hour am/pm format

A - the current time in 24-hour HH:MM format

\A :显示时间为24小时格式:HHMM

u - the username of the current user

\u :当前用户的账号名称

v - the version of bash (e.g., 4.00)

\v BASH的版本信息

V - the release of bash, version + patch level (e.g., 4.00.0)

w - Complete path of current working directory

\w :完整的工作目录名称。家目录会以 ~代替

W - the basename of the current working directory

\W :利用basename取得工作目录名称,所以只会列出最后一个目录

! - the history number of this command

# - the command number of this command

\# :下达的第几个命令

$ - if the effective UID is 0, a #, otherwise a $

\$ :提示字符,如果是root时,提示符为:# ,普通用户则为:$

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的格式:

[MCdeMacBook-Pro 5.18 mc]$echo $PS1

[\h \W \u]\$

[MCdeMacBook-Pro 5.18 mc]$PS1='[\H \w \u]\$ '

[MCdeMacBook-Pro.local /System/Library/Perl/5.18 mc]$

注意每个项目后面我添加的空格 ,否则会连在一起,很不容易读:

[MCdeMacBook-Pro.local/System/Library/Perl/5.18mc]$


Change foreground color of the prompt

改变提示符字体颜色(前景颜色)


格式为(To add colors to the shell prompt use the following export command syntax):

'\e[x;ym $PS1 \e[m’. 


PS1='\e[0;34m\u-\d-\A\$ \e[m'



PS1='\e[1;34m\]\u-\e[0;35m\]\d-\e[1;36m\]\A\$ ' 
PS1='\e[1;34m\u-\e[0;35m\d-\e[1;36m\A\$‘ (这条更好)


mc-Mon Nov 18-13:39$ ls……….



由此可见,连之后的输出也改了颜色。造成这种不理想现象的原因是,没有加上颜色结束标识\e[m

用下面这条命令:

PS1='\e[1;34m\]\u-\e[0;35m\]\d-\e[1;36m\]\A\$ \e[m

PS1='\e[1;34m\u-\e[0;35m\d-\e[1;36m\A\$ \e[m’ (这条更好)


然而这只是临时的,如果想永久改变,需要把 PS1='\e[1;34m\u-\d-\A\$ \e[m’(也就是要设置的值) 加到bashrc文件里。(在我的机器上,是/etc/bashrc), 或者,用下面的形式,更新.bash_profile or .bashrc

STARTCOLOR='\e[0;34m';

ENDCOLOR="\e[0m"

export PS1="$STARTCOLOR\u@\h \w> $ENDCOLOR"


颜色号码表(Color Code Table):


Black    0;30

Blue      0;34

Green     0;32

Cyan(青色) 0;36

Red      0;31

Purple      0;35

Brown        0;33

[Note: Replace 0 with 1 for dark color]


改变背景颜色(Change background color of the prompt)


Change the background color by specifying \e[{code}m in the PS1 prompt


PS1="\e[46m\u@\h \w> \e[m”

这样可以改变背景颜色。


联合改变字体与背景颜色:

\e[0;34m\e[47m $PS1 \e[m


mc@Mon Nov 18 ~/codes> PS1="\e[0;37m\e[44m\u@\d \w> \e[m"

mc@Mon Nov 18 ~/codes> 



这样可以按段改变前后背景:

 PS1='\e[1;34m\e[46m\u-\e[0;35m\d-\e[1;36m\A\$ \e[ 

mc-Mon Nov 18-14:52$



和改变字体颜色一样,如果想永久改变,需要更新bashrc文件。把设置值直接添加,或者用下列方式:

STARTFGCOLOR='\e[0;34m';

STARTBGCOLOR="\e[47m"

ENDCOLOR="\e[0m"

export PS1="$STARTFGCOLOR$STARTBGCOLOR\u@\h \w> $ENDCOLOR"


颜色总表


    前景      背景     颜色

    ---------------------------------------

    30          40     黑色

    31          41      红色

    32         42      绿色

    33          43      黄色

    34          44      蓝色

    35          45      紫色

    36         46      青色

    37          47      白色


    代码     意义

    -------------------------

    0         OFF

    1         高亮 highlight

    4          下划线 underline

    5         闪烁

    7         反白显示

    8          不可见

上一篇:perl 中装载自己的模块。
下一篇:类Unix系统里的/etc/shadow