Linux shell PS1设置

来源:互联网 发布:前端 后端 知乎 编辑:程序博客网 时间:2024/05/01 12:32

Linux的命令提示符的格式是由系统变量PS1和PS2来定义的。

    PS1:就是用户平时的提示符。
    PS2:第一行没输完,等待第二行输入的提示符。
设置格式如下所示:
  PS1=‘\h:\W \u\$’

其中\h \W之类的解释,在fc8下面 man bash,显示结果如下:

        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 cus-

       tomized 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




常用的几个选项参照如下:
    /d :代表日期,格式为weekday month date,例如:"Mon Aug 1"
    /H :完整的主机名称。例如:我的机器名称为:fc8.RedHat,则这个名称就是fc8.RedHat
    /h :仅取主机的第一个名字,如上例,则为fc4,.RedHat则被省略
    /t :显示时间为24小时格式,如:HH:MM:SS
    /T :显示时间为12小时格式
    /A :显示时间为24小时格式:HH:MM
    /u :当前用户的账号名称
    /v :BASH的版本信息
    /w :完整的工作目录名称。家目录会以 ~代替
    /W :利用basename取得工作目录名称,所以只会列出最后一个目录
    /# :下达的第几个命令
    /$ :提示字符,假如是root时,提示符为:# ,普通用户则为:$

----------------------------------------------------------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------------------------------------------------
    我们能够通过配置PS1变量使提示符成为彩色。在PS1中配置字符序列颜色的格式为:
    /[/e[F;Bm/]
    其中``F''为字体颜色,编号30~37;``B''为背景色,编号40~47。
    可通过``/e[0m''关闭颜色输出;特别的,当B为1时,将显示加亮加粗的文字,周详请看下面的颜色表和代码表。
    颜色表
 前景背景颜色3040黑色3141红色3242绿色3343黄色3444蓝色3545紫红色3646青蓝色3747白色


代码意义0OFF1高亮显示4下划线5闪烁7反白显示8不可见

一般普通配置是
PS1='[\u@\h \W]$'
显示结果类似如下:

-bash-3.2$ 

-bash-3.2$ 

-bash-3.2$ PS1='[\u@\h \W]$'

[carl.shen@cvs ~]$

[carl.shen@cvs ~]$



要加上颜色的话,参考网上的一个比较经典的配置。
root账户登陆显示红色,普通账户登陆显示蓝色。
将以下代码写入/etc/profile.

GROUP=`id -gn`

if [ "$USER" = "root" ]; then

        PS1='[\[\e[31;1m\]\u\e[0m@\h \W]\$'

else

        PS1='[\[\e[34;1m\]\u\e[0m@\h \W]\$'

fi

export PS1



每次启动shell的时候,都会导入上述配置。
如果要在当前shell起作用,那么需要输入
source /etc/profile