linux ps1 主机名 ip,Bash Shell PS1: 自定义你的linux提示符十例
1.提示符中顯示用戶名,主機名和當(dāng)前工作目錄
這個例子中PS1將在提示符中顯示以下三種信息\u – 用戶名
\h – 主機名
\w – 當(dāng)前工作目錄的絕對路徑-bash-3.2$ export PS1="\u@\h \w> "
ramesh@dev-db ~> cd /etc/mail
ramesh@dev-db /etc/mail>
2. 提示符中顯示當(dāng)前時間
在PS1環(huán)境變量中,你可以使用$(linux_command)這種格式直接執(zhí)行任何linux指令,在下面的例子中,通過執(zhí)行指令$(date)在提示符中顯示當(dāng)前時間ramesh@dev-db ~> export PS1="\u@\h [\$(date +%k:%M:%S)]> "
ramesh@dev-db [11:09:56]>
你也可以使用\t以hh:mm:ss格式顯示當(dāng)前的時間ramesh@dev-db ~> export PS1="\u@\h [\t]> "
ramesh@dev-db [12:42:55]>
你也可以像下面的示例一樣使用\@ 以12小時am/pm格式顯示當(dāng)前時間ramesh@dev-db ~> export PS1="[\@] \u@\h> "
[04:12 PM] ramesh@dev-db>
3. 在提示符中顯示任一linux指令輸出
你可以在提示符中顯示任何linux指令的輸出.下面的例子在提示符中顯示三項用|(pipe)分割的信息\!: 顯示歷史指令記錄數(shù)
\h: 主機名
$kernel_version: ?uname -r 指令的輸出
\$?: 上一條指令的狀態(tài)ramesh@dev-db ~> kernel_version=$(uname -r)
ramesh@dev-db ~> export PS1="\!|\h|$kernel_version|\$?> "
473|dev-db|2.6.25-14.fc9.i686|0>
4. 更改提示符的前景色
Display prompt in blue color, along with username, host and current directory information
使用藍(lán)色顯示提示符,包括用戶名,主機名和當(dāng)前工作目錄信息
$ 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 - 顏色值., 使用下面提到的數(shù)值
\e[m - 提示符顏色的結(jié)束處
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中,使其永久生效STARTCOLOR='\e[0;34m';
ENDCOLOR="\e[0m"
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 的值以更改背景色$ export PS1="\e[47m\u@\h \w> \e[m" ?[Note: This is for Light Gray background]
組合背景色和前景色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. 在提示符中顯示混合色
你也可以在同一提示符中顯示混合色,將下面的函數(shù)添加到.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函數(shù)
你也可以在PS1中像下面一樣調(diào)用一個bash shell函數(shù)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中調(diào)用一個shell腳本,在下面的例子中,~/bin/totalfilesize.sh這個腳本被PS1變量調(diào)用,以顯示當(dāng)前目錄總的文件數(shù),ramesh@dev-db ~> cat ~/bin/totalfilesize.sh
for filesize in $(ls -l . | grep "^-" | awk '{print $5}')
do
let totalsize=$totalsize+$filesize
done
echo -n "$totalsize"
ramesh@dev-db ~> export PATH=$PATH:~/bin
ramesh@dev-db ~> export PS1="\u@\h [\$(totalfilesize.sh) bytes]> "
ramesh@dev-db [534 bytes]> cd /etc/mail
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\]\\$ "
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
總結(jié)
以上是生活随笔為你收集整理的linux ps1 主机名 ip,Bash Shell PS1: 自定义你的linux提示符十例的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: aptana php,aptana 3
- 下一篇: OCCT学习遇到的问题记录