PS1参数详解

From:  http://www.safe5.com/patch/xitongloudong/linux/20120305/27116.html

bash Shell Prompt Characters Character

  \a The bell character

  \d The date in the format ‘‘Day Month Date’’

  \e The ASCII escape character

  \h The local hostname

  \H The fully qualified domain hostname

  \j The number of jobs currently managed by the shell

  \l The basename of the shell’s terminal device name

  \n The ASCII newline character

  \r The ASCII carriage return

  \s The name of the shell

  \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

  \u The username of the current user

  \v The version of the bash shell

  \V The release level of the bash shell

  \w The current working directory

  \W The basename of the current working directory

  \! The bash shell history number of this command

  \# The command number of this command

  \$ A dollar sign if a normal user, or a pound sign if the root user

  \nnn The character corresponding to the octal value nnn

  \\ A backslash

  \[ Begins a control code sequence

  \] Ends a control code sequence

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


PS1使用详解

  使用方法:

  # PS1='自定义内容'

  注意两边的单引号

  示例: PS1='(\u@\H \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  


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值