2008-09-19 81 views
116

你最喜歡的Bash提示符中有些元素是什麼?你最喜歡的Bash提示是什麼?

我喜歡有最新命令的成功與否的指標,像這樣(在.bashrc):

function exitstatus { 

    EXITSTATUS="$?" 
    BOLD="\[\033[1m\]" 
    RED="\[\033[1;31m\]" 
    GREEN="\[\e[32;1m\]" 
    BLUE="\[\e[34;1m\]" 
    OFF="\[\033[m\]" 

    PROMPT="[\[email protected]\h ${BLUE}\W${OFF}" 

    if [ "${EXITSTATUS}" -eq 0 ] 
    then 
     PS1="${PROMPT} ${BOLD}${GREEN}:)${OFF} ]\$ " 
    else 
     PS1="${PROMPT} ${BOLD}${RED}:(${OFF} ]\$ " 
    fi 

    PS2="${BOLD}>${OFF} " 
} 

PROMPT_COMMAND=exitstatus 
+2

使用同義反復的人「可能會或可能不會」,這促使我來砸他們;) – Dan 2008-09-19 19:58:15

+0

的問題是有用的,它不應該與答案合併。 – dhill 2012-02-10 14:05:58

+13

有時候我想知道。一個40K的觀點,72個upvotes,50個最喜歡的問題,其接受的答案有58個upvotes,以及「BASH best PS1」的最高谷歌成績,已被封閉爲「Not Constructive」...是的,那麼好吧。 (注意,我知道SO的規則,但規則生成例外,那麼他們呢?) – 2015-01-12 22:30:42

回答

16

這裏是我的。

# this prompt will show the hostname in green if the last command returned 0, 
# otherwise it will be red. 
PS1="\[\`if [[ \$? = "0" ]]; then echo '\e[32m\h\e[0m'; else echo '\e[31m\h\e[0m' ; fi\`:\w\n\$ 

看起來像這樣'筆記本電腦'是綠色的,其餘的默認終端前景,當然。

laptop:~/bin 
$ 
+2

如果在美元提示符之前有新行,但是如果要刪除該行,則需要使用\\ [`和`\\``將所有非打印控制字符括起來。否則,長命令將無法正確包裝。 – dreeves 2010-06-20 02:28:26

3

我的提示:

[週五9月19日上午10時33分]
[NAT @谷〜] $

#################################3 
## File used for defining $PS1 

bash_prompt_command() { 
# How many characters of the $PWD should be kept 
local pwdmaxlen=25 
# Indicate that there has been dir truncation 
local trunc_symbol=".." 
local dir=${PWD##*/} 
pwdmaxlen=$(((pwdmaxlen < ${#dir}) ? ${#dir} : pwdmaxlen)) 
NEW_PWD=${PWD/#$HOME/\~} 
local pwdoffset=$((${#NEW_PWD} - pwdmaxlen)) 
if [ ${pwdoffset} -gt "0" ] 
then 
    NEW_PWD=${NEW_PWD:$pwdoffset:$pwdmaxlen} 
    NEW_PWD=${trunc_symbol}/${NEW_PWD#*/} 
fi 
} 

bash_prompt() { 
local NONE="\[\033[0m\]" # unsets color to term's fg color 

# regular colors 
local K="\[\033[0;30m\]" # black 
local R="\[\033[0;31m\]" # red 
local G="\[\033[0;32m\]" # green 
local Y="\[\033[0;33m\]" # yellow 
local B="\[\033[0;34m\]" # blue 
local M="\[\033[0;35m\]" # magenta 
local C="\[\033[0;36m\]" # cyan 
local W="\[\033[0;37m\]" # white 

# empahsized (bolded) colors 
local EMK="\[\033[1;30m\]" 
local EMR="\[\033[1;31m\]" 
local EMG="\[\033[1;32m\]" 
local EMY="\[\033[1;33m\]" 
local EMB="\[\033[1;34m\]" 
local EMM="\[\033[1;35m\]" 
local EMC="\[\033[1;36m\]" 
local EMW="\[\033[1;37m\]" 

# background colors 
local BGK="\[\033[40m\]" 
local BGR="\[\033[41m\]" 
local BGG="\[\033[42m\]" 
local BGY="\[\033[43m\]" 
local BGB="\[\033[44m\]" 
local BGM="\[\033[45m\]" 
local BGC="\[\033[46m\]" 
local BGW="\[\033[47m\]" 

local UC=$W     # user's color 
[ $UID -eq "0" ] && UC=$R # root's color 

# without colors: PS1="[\[email protected]\h \${NEW_PWD}]\\$ " 
# extra backslash in front of \$ to make bash colorize the prompt 

#Nat's Colored Prompt 
PS1="${EMK}\n[${EMR}\d ${EMR}\@${EMK}] \n${EMK} [${UC}\u${EMK}@${UC}\h ${EMB}\${NEW_PWD}${EMK}]${UC}\\$ ${NONE}" 


} 

PROMPT_COMMAND=bash_prompt_command 
bash_prompt 
unset bash_prompt  
0
PS1="\n\e[30;1mUSER (\e[0m\e[33;1m\w\e[0m\e[30;1m)\e[0m\e[33;33m\n$ \e[0m" 
2

我用Glandium.org的「Adding some VCS information in bash prompt的定製版本「提示。我所做的最重要的事情是將VCS收集位分成一個單獨的腳本。

此提示非常好,因爲它提供了大量關於我正在處理的項目的信息,我需要了解有關基於VCS信息的項目。我還定製了我的顯示命令返回結果,屏幕窗口號,用戶和機器名稱以及當前時間。它很長(42個字符或更多),但我使用寬端子,所以這不是什麼大問題。

舉例來說,我現在的提示(位編輯後)看起來像:

[0-1][email protected](g:proj1[topic/fix-123]app)[10:50]|$ 

這意味着最後一個命令有了結果爲0,在屏幕窗口1,邁克爾登錄到HAL。當前目錄是proj1的Git工作目錄,在上午10點50分目錄應用程序中的分支主題/ fix-123內。以常規用戶身份登錄。如果有人想要我的自定義腳本,請讓我知道...我會很高興把它放在這裏或其他地方很容易訪問。

+0

優秀。正是我在找什麼。你能上傳嗎? – demonkoryu 2011-01-20 08:17:04

22

in。bashrc中:

PS1='\$ ' 

(H/T @bitmask用於編輯)

0

我想保持它的(相對)簡單:

用戶名@主機名:/充滿/路徑>

PS1='\[email protected]\h:\w> ' 
export PS1 
2

我有這個作爲我的很長一段時間的bash提示:

 
PS1="(\d \t) (\[email protected]\h:\w)\nbash> " 
export PS1 
0

礦,在家裏,是非常簡單的:

[[email protected]:/full/CWD]$

在工作中,我們的生產和測試用戶環境「概念,並及時爲

(env;sub-env) [[email protected]:/full/CWD]$
18

因爲我一跳在我的開發工作站,開發服務器和臨時環境之間,我已經在提示中用下面的代碼片段着色了機器名,以便在我看着顯示器上的術語海時給出一個可視指示器他們連接了哪些機器:

# Color the hostname 
if [ $HOSTNAME = 'claudius' ]; then 
    export HOST_COLOR="\[\033[1;36m\]" 
fi 
if [ $HOSTNAME = 'drooble' ]; then 
    export HOST_COLOR="\[\033[1;34m\]" 
fi 
if [ $HOSTNAME = 'davinci' ]; then 
    export HOST_COLOR="\[\033[1;31m\]" 
fi 

# Color the colon red if root 
COLON_COLOR='0m' 
if [ ${UID} -eq 0 ]; then 
    COLON_COLOR='1;31m' 
fi 

然後,與彩色主機名的全PS1變量,結腸癌(紅色如果根,即,須藤-s)和完整的路徑:

PS1=`echo -ne "$HOST_COLOR\h\[\033[00m\]\[\e[$COLON_COLOR\]:\[\033[01;32m\]\w\[\033[00m\]\\[\033[01;33m\]\$\[\033[00m\] "` 
0

我喜歡這個:

\[\033[33m\][\[email protected]\[\033[1;31m\]\h]\]\033[0m {\W}\n\033[1;34m\]\w\]\033[0m > 

它把

[name]@[hostname] 
[pwd] > 

[name]黃色,[hostname]紅色和[pwd]在藍色

1

礦:

function prompt_err { 
    if test "$?" -eq 0; then PS1=': \W$; '; else PS1=': \W [ERROR#$?]$; '; fi 
} 
PROMPT_COMMAND=prompt_err 
PS2=' ' 

請問:

  • 顯示目錄名
  • 允許對整條生產線複製/粘貼到另一個shell重新執行相同的命令,忽視提示
  • 如果以前的命令退出狀態不是0,則打印一個可見的錯誤狀態碼
  • 次提示符的空間,以允許複製/粘貼
2

我修改了原來的海報的例子了一下:

function exitstatus { 

    EXITSTATUS="$?" 
    BOLD="\[\033[1m\]" 
    RED="\[\033[0;31m\]" 
    GREEN="\[\e[0;32m\]" 
    BLUE="\[\e[34m\]" 
    OFF="\[\033[m\]" 

    HOST="\h" 
    USER="\u" 
    DIR="\w" 
    NEWLINE="\n" 
    DATE="\d" 
    TIME="\t" 

    PROMPT="\[\033]0;${USER}@${HOST}: \w\007\n${RED}${TIME} ${DATE} [${USER}@${HOST}:[${BLUE}\w${RED}]" 

    if [ "${EXITSTATUS}" -eq 0 ] 
    then 
     PS1="${PROMPT} [${GREEN}${EXITSTATUS}${RED}]${OFF}\n$ " 
    else 
     PS1="${PROMPT} [${BOLD}${EXITSTATUS}${RED}]${OFF}\n$ " 
    fi 

    PS2="${BOLD}>${OFF} " 
} 

PROMPT_COMMAND=exitstatus 
10

我喜歡的提示,告訴大約多久以前的命令把執行。 像這樣:

0:007 /home/jcl 0$ sleep 1
1:012 /home/jcl 0$

它可以這樣實現:

bold='\[\e[1m\]' 
plain='\[\e[0m\]' 

set_begin() 
{ 
    if [ -z "$begin" ] 
    then 
    begin="$(date +"%s %N")" 
    fi 
} 

calc_elapsed() 
{ 
    read begin_s begin_ns <<< "$begin" 
    begin_ns="${begin_ns##+(0)}" 
    # PENDING - date takes about 11ms, maybe could do better by digging in 
    # /proc/$$. 
    read end_s end_ns <<< $(date +"%s %N") 
    end_ns="${end_ns##+(0)}" 
    local s=$((end_s - begin_s)) 
    local ms 
    if [ "$end_ns" -ge "$begin_ns" ] 
    then 
    ms=$(((end_ns - begin_ns)/1000000)) 
    else 
    s=$((s - 1)) 
    ms=$(((1000000000 + end_ns - begin_ns)/1000000)) 
    fi 
    elapsed="$(printf " %2u:%03u" $s $ms)" 
    if [ "$s" -ge 300 ] 
    then 
    elapsed="$elapsed [$(human_time $s)]" 
    fi 
} 



human_time() 
{ 
    local s=$1 
    local days=$((s/(60*60*24))) 
    s=$((s - days*60*60*24)) 
    local hours=$((s/(60*60))) 
    s=$((s - hours*60*60)) 
    local min=$((s/60)) 
    if [ "$days" != 0 ] 
    then 
    local day_string="${days}d " 
    fi 
    printf "$day_string%02d:%02d\n" $hours $min 
} 

timer_prompt() 
{ 
    status=$? 
    local size=16 
    calc_elapsed 
    if [ "${#PWD}" -gt $size ] 
    then 
    pwd_string="${PWD: -$size}" 
    else 
    pwd_string="$(printf "%${size}s" $PWD)" 
    fi 
    PS1="$bold$elapsed $pwd_string $status\\$ $plain" 
    begin= 
} 

set_begin 
trap set_begin DEBUG 
PROMPT_COMMAND=timer_prompt 
相關問題