Chinaunix首页 | 论坛 | 博客
  • 博客访问: 798856
  • 博文数量: 76
  • 博客积分: 2211
  • 博客等级: 上尉
  • 技术积分: 1693
  • 用 户 组: 普通用户
  • 注册时间: 2011-12-26 19:36
文章分类

全部博文(76)

文章存档

2012年(67)

2011年(9)

分类: LINUX

2011-12-30 11:08:43

# PS1='自定义内容'
    注意两边的单引号
    示例: PS1='( /d /t)/$'
    ----------------------------------------------------------------------------------------------------------------
    PS1:就是用户平时的提示符。
    PS2:第一行没输完,等待第二行输入的提示符。
    Linux系统提示符是用系统变量PS1来定义的。一般系统默认的形式是:[username@host 工作目录]$.
    用echo $PS1能够得到PS1的值,即PS1="[/u@/h /w]"/$
    登录后能够更改PS1的显示样式,但是当退出重启登录进入系统后,样式又变成系统默认的样式了,假如要完全改变他的样式,只能从配置文档中改。
    PS是在用户根目录下的.bash_profile中定义的。
    如
    # .bash_profile
    # Get the aliases and functions
    if [ -f ~/.bashrc ]; then
    . ~/.bashrc
    fi
    # User specific environment and startup programs
    #以下是设定的PS1的值
    PS1="[/u@/h /w]/$"
    PATH=$PATH:$HOME/bin
    #使用export把PS1输出,以使他能够在子shell中生效,这会造成ROOT用户的也采用此样式
    #export PS1 要慎用
    export PATH
    unset USERNAME
    下面简单说说环境下默认的特别符号所代表的意义:
    /d :代表日期,格式为weekday month date,例如:"Mon Aug 1"
    /H :完整的主机名称。例如:我的机器名称为:fc4.linux,则这个名称就是fc4.linux
    /h :仅取主机的第一个名字,如上例,则为fc4,.linux则被省略
    /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时,将显示加亮加粗的文字,周详请看下面的颜色表和代码表。
    颜色表
    前景 背景 颜色
    ---------------------------------------
    30 40 黑色
    31 41 色
    32 42 色
    33 43 色
    34 44 色
    35 45 紫色
    36 46 青色
    37 47 白色
    代码 意义
    -------------------------
    0 OFF
    1 高亮显示
    4 underline
    5 闪烁
    7 反白显示
    8 不可见

    假如想要配置终端提示符的样式只要把$PS1在~/.bahrc指定即可比,比如我的配置如下:
    配置一:
    PS1="/[/e[32m/][/u@/h /w]$/[/e[m/]"
    export PS1
    效果:
    [LinuxSong@test ~]$
    配置二:
    if [ $TERM = 'linux' ]; then
    export PS1="[/[/e[36;1m/]/u@/h /[/e[31;1m/]/w/[/e[32;1m/]]> "
    zhcon --utf8
    clear
    elif [ $TERM = "xterm" ]; then
    export PS1="[/[/e[31;1m/]/w/e[0m]> "
    fi
===========================
===========================

\d :代表日期,格式为weekday month date,例如:"Mon Aug 1"
\H :完整的主机名称。例如:我的机器名称为:fc4.linux,则这个名称就是fc4.linux
\h :仅取主机的第一个名字,如上例,则为fc4,.linux则被省略
\t :显示时间为24小时格式,如:HH:MM:SS
\T :显示时间为12小时格式
\A :显示时间为24小时格式:HH:MM
\u :当前用户的账号名称
\v :BASH的版本信息
\w :完整的工作目录名称。家目录会以 ~代替
\W :利用basename取得工作目录名称,所以只会列出最后一个目录
\# :下达的第几个命令
\$ :提示字符,如果是root时,提示符为:# ,普通用户则为:$
    CentOS中关于这些:
\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="\u@\h:\w\$ "
    但是这样设置之后显示的提示符是没有颜色的,如果希望加入颜色设置,可以这样:
PS1="\e[1;32m\u\e[m\e[1;33m@\e[m\e[1;35m\h\e[m:\w\$ "
    此时,在终端中的显示应该是彩色的提示符了,其中的\e[1;xxm和\e[m就是改变终端字体颜色的部分,前者是修改颜色,后者是恢复到正常显示。但是这时还会有问题,在提示符中输入第一行时,回车换行不正常,导致显示很混乱,因此,除了这个修改之外,还需要将全部非打印字符用专用的 bash 转义序列 "\[" 和 "\]" 括起来。这两个序列通知 bash,被括起来的字符不占用行上的任何空间,这样就使自动换行能够继续正常工作。最终版本如下(可解决带颜色提示符的不换行问题):
PS1="\[\e[1;32m\]\u\[\e[m\]\[\e[1;33m\]@\[\e[m\]\[\e[1;35m\]\h\[\e[m\]:\w\$ "
 
 
阅读(4936) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~