1. 提示符中显示用户名,主机名和当前工作目录
这个例子中PS1将在提示符中显示以下三种信息
- \u – 用户名
- \h – 主机名
- \w – 当前工作目录的绝对路径
1
| -bash-3.2$ export PS1="\u@\h \w> " |
1
| ramesh@dev-db ~> cd /etc/mail |
1
| ramesh@dev-db /etc/mail> |
2. 提示符中显示当前时间
在PS1环境变量中,你可以使用$(linux_command)这种格式直接执行任何linux指令,在下面的例子中,通过执行指令$(date)在提示符中显示当前时间
1
| ramesh@dev-db ~> export PS1="\u@\h [\$(date +%k:%M:%S)]> " |
1
| ramesh@dev-db [11:09:56]> |
你也可以使用\t以hh:mm:ss格式显示当前的时间
1
| ramesh@dev-db ~> export PS1="\u@\h [\t]> " |
1
| ramesh@dev-db [12:42:55]> |
你也可以像下面的示例一样使用\@ 以12小时am/pm格式显示当前时间
1
| ramesh@dev-db ~> export PS1="[\@] \u@\h> " |
1
| [04:12 PM] ramesh@dev-db> |
3. 在提示符中显示任一linux指令输出
你可以在提示符中显示任何linux指令的输出.下面的例子在提示符中显示三项用|(pipe)分割的信息
- \!: 显示历史指令记录数
- \h: 主机名
- $kernel_version: uname -r 指令的输出
- \$?: 上一条指令的状态
1
| ramesh@dev-db ~> kernel_version=$(uname -r) |
1
| ramesh@dev-db ~> export PS1="\!|\h|$kernel_version|\$?> " |
1
| 473|dev-db|2.6.25-14.fc9.i686|0> |
4. 更改提示符的前景色
Display prompt in blue color, along with username, host and current directory information
使用蓝色显示提示符,包括用户名,主机名和当前工作目录信息
$ export PS1="\e[0;34m\u@\h \w> \e[m" [Note: This is for light blue prompt] $ export PS1="\e[1;34m\u@\h \w> \e[m" [Note: This is for dark blue prompt]
- \e[ - 提示符颜色的开始处
- x;ym - 颜色值., 使用下面提到的数值
- \e[m - 提示符颜色的结束处
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]
将下面指令添加到.bash_profile或者.bashrc中,使其永久生效
1
| export PS1="$STARTCOLOR\u@\h \w> $ENDCOLOR" |
5. 更改提示符背景色
Change the background color by specifying \e[{code}m in the PS1 prompt as shown below.
想下面的例子一样在PS1中指定\e[{code}m 的值以更改背景色
1
| $ export PS1="\e[47m\u@\h \w> \e[m" |
[Note: This is for Light Gray background]
组合背景色和前景色
1
| export PS1="\e[0;34m\e[47m\u@\h \w> \e[m" |
[Note: This is for Light Blue foreground and Light Gray background]
将下面的代码添加到.bash_profile或者.bashrc中使背景色和前景色永久生效.
STARTFGCOLOR='\e[0;34m'; STARTBGCOLOR="\e[47m" ENDCOLOR="\e[0m" export PS1="$STARTFGCOLOR$STARTBGCOLOR\u@\h \w> $ENDCOLOR"
从下面的背景色中选择最符合你口味儿的颜色.
- \e[40m
- \e[41m
- \e[42m
- \e[43m
- \e[44m
- \e[45m
- \e[46m
- \e[47m
6. 在提示符中显示混合色
你也可以在同一提示符中显示混合色,将下面的函数添加到.bash_profile中
function prompt { local BLUE="\[\033[0;34m\]" local DARK_BLUE="\[\033[1;34m\]" local RED="\[\033[0;31m\]" local DARK_RED="\[\033[1;31m\]" local NO_COLOR="\[\033[0m\]" case $TERM in xterm*|rxvt*) TITLEBAR='\[\033]0;\u@\h:\w\007\]' ;; *) TITLEBAR="" ;; esac PS1="\u@\h [\t]> " PS1="${TITLEBAR}\ $BLUE\u@\h $RED[\t]>$NO_COLOR " PS2='continue-> ' PS4='$0.$LINENO+ ' }
你可以重新登陆使修改生效,或者像下面一样source .bash_profile
$. ./.bash_profile $ prompt ramesh@dev-db [13:02:13]>
7. 用tput更改提示符颜色
你也可以像下面一样在PS1中使用tput更改颜色
$ export PS1="\[$(tput bold)$(tput setb 4)$(tput setaf 7)\]\u@\h:\w $ \[$(tput sgr0)\]"
tput Color Capabilities:
- tput setab [1-7] - Set a background color using ANSI escape
- tput setb [1-7] - Set a background color
- tput setaf [1-7] - Set a foreground color using ANSI escape
- tput setf [1-7] - Set a foreground color
tput Text Mode Capabilities:
- tput bold - Set bold mode
- tput dim - turn on half-bright mode
- tput smul - begin underline mode
- tput rmul - exit underline mode
- tput rev - Turn on reverse mode
- tput smso - Enter standout mode (bold on rxvt)
- tput rmso - Exit standout mode
- tput sgr0 - Turn off all attributes
Color Code for tput:
- 0 - Black
- 1 - Red
- 2 - Green
- 3 - Yellow
- 4 - Blue
- 5 - Magenta
- 6 - Cyan
- 7 - White
8. 使用下面的PS1变量可用代码制作个性化的提示符
使用下面的代码制作一个符合你自己口味儿和所需要功能的个性化提示符.
- \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 part
- \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 character
9. 在PS1变量中使用bash shell函数
你也可以在PS1中像下面一样调用一个bash shell函数
ramesh@dev-db ~> function httpdcount { > ps aux | grep httpd | grep -v grep | wc -l > } ramesh@dev-db ~> export PS1="\u@\h [`httpdcount`]> " ramesh@dev-db [12]> [Note: This displays the total number of running httpd processes]
你可以将下面的代码添加到.bash_profile或者.bashrc中使其永久生效
function httpdcount { ps aux | grep httpd | grep -v grep | wc -l } export PS1='\u@\h [`httpdcount`]> '
10. 在PS1变量中使用shell脚本
你也可以在PS1中调用一个shell脚本,在下面的例子中,~/bin/totalfilesize.sh这个脚本被PS1变量调用,以显示当前目录总的文件数,
1
| ramesh@dev-db ~> cat ~/bin/totalfilesize.sh |
1
| for filesize in $(ls -l . | grep "^-" | awk '{print $5}') |
1
| let totalsize=$totalsize+$filesize |
1
| ramesh@dev-db ~> export PATH=$PATH:~/bin |
1
| ramesh@dev-db ~> export PS1="\u@\h [\$(totalfilesize.sh) bytes]> " |
1
| ramesh@dev-db [534 bytes]> cd /etc/mail |
1
| ramesh@dev-db [167997 bytes]> |
11. IP地址示例
my_ip=$(/sbin/ip addr show dev eth1 | perl -ne '/inet ([\d.]+)/ && print $1')
export PROMPT_COMMAND='echo -ne "\033]0;$my_ip\007"'
export PS1="\[\e[1;36m\][\H]\[\e[31;1m\]\u\[\e[0m\]@\[\e[32;1m\]$my_ip\[\e[0m\]:\[\e[35;1m\]\w\[\e[0m\]\\$ "
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>