设置bash终端提示符(PS1)

来源:互联网 发布:ug编程培训 编辑:程序博客网 时间:2024/05/01 05:10
原文见:How to: Change/Setup bash custom prompt(PS1)
  1. 定制shell提示符的提示信息
  2. 给shell提示符添加颜色

定制shell提示符的提示信息

Bash是以转义字符\加上相应字符来定制提示信息,相应的含义如下表:
  • \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
For example: PS1=Output\d \h $Sat May 05 Button[\d \t \u@\h:\W ] $ "[Sat May 05 22:18:30 xiayun@Button: ~ ] $

给shell提示符添加颜色

给shell提示符添加颜色有两种方法:

方法一:

给shell提示符添加颜色的语法:'\e[x;ym $PS1 \e[m'
  • \e[ Start color scheme
  • x;y Color pair to use (x;y)
  • $PS1 is your shell prompt
  • \e[m Stop color theme

To set a red color prompt, add this into .bashrc
PS='\e[0;31m[\u@\h \W]\$ \e[m '

ColorCodeBlack0;30Blue0;34Green0;32Cyan0;36Red0;31Purple0;35Brown0;33Blue0;34Green0;32Cyan0;36Red0;31Purple0;35Brown0;33

方法二:

tput command

使用tput命令。例如显示红色的提示符:
PS1=’\[$(tput setaf 1)\]\u@\h:\W $ \[$(tput sgr0)\]'

tput命令的使用有

  • tput bold - Bold effect
  • tput rev - Display inverse colors
  • tput sgr0 - Reset everything
  • tput setaf {CODE}- Set foreground color, see color {CODE} below
  • tput setab {CODE}- Set background color, see color {CODE} below

Colors {code} code for tput command

Color {code}Color0Black1Red2Green3Yellow4Blue5Magenta6Cyan7White

Exampe: Open /etc/bashrc (Redhat and friends) / or /etc/bash.bashrc (Debian/Ubuntu) or /etc/bash.bashrc.local (Suse and others) file and append following code:# vi /etc/bashrcor$ sudo gedit /etc/bashrcAppend the code as follows# If id command returns zero, you’ve root access.if [ $(id -u) -eq 0 ];then # you are root, set red colour prompt PS1="\\[$(tput setaf 1)\\]\\u@\\h:\\w #\\[$(tput sgr0)\\]"else # normal PS1="[\\u@\\h:\\w] $"fiSave and source the file.
Read the man page of bash and tput command for more information

原创粉丝点击