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
總結
- 上一篇: 一个托盘程序演示 -闹钟 Alert
- 下一篇: gulp版本号管理插件注意事项