如何在Linux VPS上定製自己的Bash Prompt

來源:互聯網
上載者:User

標籤:empty   使用   variable   org   分發   開頭   底線   許可權   als   

提供:ZStack雲端運算

內容介紹

在管理Linux伺服器時,命令列操作無疑是最為耗時的環節。對大多數使用者而言,這意味著將大量時間用於操作Bash shell。

儘管大多數發行版都提供預設的使用者類型與root prompts,但定製我們自己的prompt無疑有助於引入更多使用偏好。大家可以在其中納入各類實用資訊,協助自己更有針對性地執行任務並在許可權提升時得到提示。

在本樣本中,我們將使用Ubuntu 12.04 VPS,但幾乎所有現代Linux發行版都遵循類似的操作方法。

驗證您的Shell為Bash

在著手定製shell之前,大家應當確認自己的當前shell為Bash。

對於大多數系統而言這一點並不成問題,但有時候某些改造版可能會使用不同的shell,或者有使用者利用其測試新shell。

檢查/etc/passwd檔案即可輕鬆確認這一點,首先開啟該檔案:

less /etc/passwd

此檔案中的每一行都包含與不同使用者相關的資訊。在第一列中找到我們的使用者及root使用者,其間以冒號分隔。在最後一個欄位內,預設登入shell為:

root:x:0:0:root:/root:/bin/bash. . .demouser:x:1000:1000:,,,:/home/demouser/bin/bash

如果最後欄位為/bin/bash, 則設定完成。

如果最後欄位並非/bin/bash,大家又希望將預設shell變更為Bash,則可利用root許可權編輯此檔案並對與使用者關聯的最後欄位加以變更:

sudo nano /etc/passwd

在變更完成後,退出登入並返回以使用Bash shell。

查看當前值

首先,我們先看看設定檔中存在哪些現有Bash prompts定義。

Bash利用PS1與PS2環境變數對其prompt進行配置。

PS1負責定義我們查看到的主prompt。在預設情況下,Ubuntu系統中的格式如下:

[email protected]: current_directory$

請注意結尾處的$號。這意味著該shell屬於普通使用者shell。對於root使用者,結尾處將以#號表示。

PS2 prompt則用於多行命令。大家可以在終端中使用以下命令以查看當前PS2變數:

echo 

直接按下斷行符號以查看該prompt。通常來講,我們定義的這些變數會儲存在~/.bashrc檔案當中,其會在我們的互操作shell啟動時接受讀取。

在Ubuntu 12.04的該檔案中,我們可以找到以下部分:

# uncomment for a colored prompt, if the terminal has the capability; turned# off by default to not distract the user: the focus in a terminal window# should be on the output of commands, not on the prompt# force_color_prompt=yesif [ -n "$force_color_prompt" ]; thenif [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then    # We have color support; assume it‘s compliant with Ecma-48    # (ISO/IEC-6429). (Lack of such support is extremely rare, and such    # a case would tend to support setf rather than setaf.)    color_prompt=yeselse    color_prompt=fifiif [ "$color_prompt" = yes ]; thenPS1=‘${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\[email protected]\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ ‘elsePS1=‘${debian_chroot:+($debian_chroot)}\[email protected]\h:\w\$ ‘fiunset color_prompt force_color_prompt

我們能夠以彩色方式標註部分邏輯。大家需要取消force_color_prompt=yes行的注釋以實現彩色prompt。這種方式還有其它好處,我們將在稍後具體討論。

force_color_prompt=yes

這裡我們需要關注的是與prompt設定相關的部分。是否使用彩色顯示依靠if-else中的嵌套機制實現:

if [ "$color_prompt" = yes ]; thenPS1=‘${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\[email protected]\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ ‘elsePS1=‘${debian_chroot:+($debian_chroot)}\[email protected]\h:\w\$ ‘fiunset color_prompt force_color_prompt

以上部分添加了彩色支援。下面來看第二部分,先排除彩色選項,看看其中的基本內容:

PS1=‘${debian_chroot:+($debian_chroot)}\[email protected]\h:\w\$ ‘

看起來相當複雜,而且存在著一些與常規shell使用似乎並不相關的部分。

其中debian_chroot代表我們是否在變更root環境下進行操作,prompt會被修改以提醒使用者。大家可能需要保留這部分提示。

其餘部分的prompt定義則為:

\[email protected]\h:\w\$: 

這部分負責表明我們在主prompt中使用了部分逸出序列。

Bash逸出序列

我們可以在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 (uses the value of the PROMPT_DIRTRIM variable)\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

如大家所見,其中包含部分基本資料,也有一些我們用不到的資訊)ASCII bell字元、Bash版本等)。

我們的prompt目前已經擁有了使用者名稱(\u)、一個@符號、主機名稱的第一部分(\h)、當前工作目錄(\w)以及用於標註普通使用者與root使用者的$與#。

下面退出~/.bashrc檔案並測試其它選項。

測試新的Bash Prompts

儘管我們最終需要編輯~/.bashrc檔案以調整選定參數,但直接通過命令列變更prompt進行效果測試顯然更為簡便。

在開始著手修改之前,我們將PS1當前值儲存在一條新變數中。如此一來,我們就能夠在無需退出登入的前提下隨時切換回原始prompt。

ORIG=$PS1

現在我們擁有了一條名為ORIG的環境變數,其中儲存著prompt的預設副本。如果我們需要切換回初始prompt,則:

PS1=$ORIG

先從簡單內容著手,為當前prompt提供使用者名稱與$:

PS1="\u$"

返回結果如下:

demouser$

加上個空格來改善顯示效果:

PS1="\u $: "demouser $:

不過我們可能不想使用$字元,這裡可以使用\$逸出序列作為替代。我們可以使用root許可權對PS1進行直接修改:

PS1="\u \$: "

接下來添加任意希望在prompt中使用的文字字元:

PS1="Hello, my name is \u! \$: "Hello, my name is demouser! $:

我們也可以利用普通shell功能插入任意命令的執行結果。

在這裡,我們可以利用反引號插入命令結果,此結果提取自/proc/loadavg處負載指標的第一列,代表我們伺服器的當前負載:

PS1="\u, load: `cat /proc/loadavg | awk ‘{ print $1; }‘` \$: "demouser, load: 0.01 $:

這樣我們就能輕鬆瞭解系統的當前工作強度了。

如果大家希望在prompt中關注資料或者時間,也可以採取類似的操作。我們需要利用小括弧與中括弧對資料位元進行分享,同時添加\w保證只追蹤當前工作目錄:

PS1="[\[email protected]\h, load: `cat /proc/loadavg | awk ‘{ print $1; }‘`] (\d - \t) \w \$ "[[email protected], load: 0.01] (Thu Feb 20 - 13:15:20) ~ $

很明顯,邊種處理方式比較麻煩,特別是在需要在長路徑之間進行目錄變更的情況下:

cd /etc/systemd/system/multi-user.target.wants[[email protected], load: 0.01] (Thu Feb 20 - 13:18:28) /etc/systemd/system/multi-user.target.wants $

如果我們仍然需要完整資訊,但希望讓命令更短一點,則可以利用\n將兩行資訊加以拆分:

PS1="[\[email protected]\h, load: `cat /proc/loadavg | awk ‘{ print $1; }‘`] (\d - \t)\n\w \$ "[[email protected], load: 0.00] (Thu Feb 20 - 13:20:00)/etc/systemd/system/multi-user.target.wants $

有些朋友不喜歡使用多行prompt,但這確實能夠讓我們的prompt容納更多資訊。

變更Prompt顏色

現在我們已經瞭解了多種prompts調整方式,接下來再給它添加一點色彩。

Bash允許我們使用特定代碼在prompt中實現彩色顯示。但這通常會帶來新的困擾,因為這部分代碼的說明內容不夠清晰。

在使用色彩調節代碼前,我們應當首先瞭解Bash設定中正確與錯誤的色彩代碼定義方式。

首先,大家必須以[與]作為色彩代碼的描述範圍。對於Bash,這代表兩個括弧間的字元為非輸出字元。

Bash需要在此基礎上估算字元數量,以備後續輸出。如果不將色彩代碼納入[與]之間,那麼Bash會將全部字元都計為文本字元並在下一行中進行打包。

另外,在括弧內的非輸出序列中,我們需要輸入\e[或者\033[指定彩色prompt的起點。二者的作用相同,都負責指定該反義序列的起始位置。在本樣本中,我們將使用\e[。

在]之前,我們還需要使用“m”來表示即將提供一條色彩序列。

基本上,每次進行色彩修改時,我們都需要輸入下面這種命令格式:

\[\e[color_informationm\]

可以看到,這會讓我們的prompts變得相當淩亂。

下面來看用於變更前景文本顏色的基本代碼:

  • 30: Black
  • 31: Red
  • 32: Green
  • 33: Yellow
  • 34: Blue
  • 35: Purple
  • 36: Cyan
  • 37: White

大家也可以通過在以上設定前設定“屬性”修改這些基礎值,各值之間以分號分隔。

根據實際終端的不同,操作效果也有所區別。部分常見屬性包括:

  • 0: 普通文本
  • 1: 在不同終端中可能代表粗體或者淺色顯示
  • 4: 底線文本

因此如果大家希望使用底線綠色文本,則:

\[\e[4;32m\]

接下來繼續正常使用即可。另外,我們也可以隨時將色彩重設為初始值。

重設命令如下:

\[\e[0m\]

結合來看,包含有使用者名稱及主機的簡單彩色prompt設定方式如下所示:

PS1="\[\e[4;32m\]\[email protected]\h\[\e[0m\]$ "

我們也可以指定背景顏色。背景顏色無法擷取屬性,具體包括:

  • 40: Black background
  • 41: Red background
  • 42: Green background
  • 43: Yellow background
  • 44: Blue background
  • 45: Purple background
  • 46: Cyan background
  • 47: White background

不過大家可以一次性指定背景顏色、屬性與文本顏色:

\[\e[42;1;36m\]

當然,這裡建議各位將背景資訊與其它資訊分隔開來:

\[\e[42m\]\[\e[1;36m\]

在使用普通文字屬性(0)時,終端中可能出現一些亂碼。如果遇到這種問題,大家最好避免使用0值指定普通屬性——由於屬於預設值,我們無需額外指定。

永久修改Prompt

在經過一系列摸索後,相信大家已經基本瞭解到自己喜愛的prompt應該是什麼樣子。

在本樣本中,我們將使用以下彩色prompt:

PS1="[\[\e[0;32m\]\[email protected]\h, load: `cat /proc/loadavg | awk ‘{ print $1; }‘`\[\e[00m\]] (\[\e[00;35m\]\d - \t\[\e[00m\])\n\w \$ "

而在需要使用音色方案時,我們則選擇以下配置:

PS1="[\[email protected]\h, load: `cat /proc/loadavg | awk ‘{ print $1; }‘`] (\d - \t)\n\w \$ "

現在我們的兩套prompt版本已經就緒,接下來在~/.bashrc檔案中編輯PS1。

nano ~/.bashrc

正如文章開頭所提到,檔案中的各prompts被包含在功能中以適用於chroot環境。這部分內容不要變動,檔案內容如下:

if [ "$color_prompt" = yes ]; thenPS1=‘${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\[email protected]\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ ‘elsePS1=‘${debian_chroot:+($debian_chroot)}\[email protected]\h:\w\$ ‘fiunset color_prompt force_color_prompt

注釋掉當前PS1分配機制,同時複製其下方的debian_chroot邏輯,修改後內容如下:

if [ "$color_prompt" = yes ]; then# PS1=‘${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\[email protected]\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ ‘PS1=‘${debian_chroot:+($debian_chroot)}‘else# PS1=‘${debian_chroot:+($debian_chroot)}\[email protected]\h:\w\$ ‘PS1=‘${debian_chroot:+($debian_chroot)}‘fiunset color_prompt force_color_prompt

在該prompt結尾處的最後一個引號之前,我們可以添加希望實現的各prompts。另外,由於我們的prompt使用單引號,因此這裡需要變更當前prompt中的參考型別以使用雙引號。

在第一條PS1分配內容中使用prompt的彩色版本。而第二條中,使用單色版本。

if [ "$color_prompt" = yes ]; then# PS1=‘${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\[email protected]\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ ‘PS1="${debian_chroot:+($debian_chroot)}[\[\e[0;32m\]\[email protected]\h, load: `cat /proc/loadavg | awk ‘{ print $1; }‘`\[\e[00m\]] (\[\e[00;35m\]\d - \t\[\e[00m\])\n\w \$ "else# PS1=‘${debian_chroot:+($debian_chroot)}\[email protected]\h:\w\$ ‘PS1="${debian_chroot:+($debian_chroot)}[\[email protected]\h, load: `cat /proc/loadavg | awk ‘{ print $1; }‘`] (\d - \t)\n\w \$ "fiunset color_prompt force_color_prompt

完成後儲存並退出。

現在在退出登入並重新登入時,我們的prompt即會根據所設定值進行變更。

總結

我們可以通過多種方式實現個人化配置,而對特定條目進行彩色顯示也有助於翻閱過程中及時關注重要內容。

另一種比較流行的作法是為root使用者提供特殊效果提示,從而提醒大家當前正處於特權操作模式。當然,各位也可以充分發揮想象力,協助自己更為高效地在混亂的工作中找到真正值得關注的資訊。

本文來源自DigitalOcean Community。英文原文:How to Customize your Bash Prompt on a Linux VPS By Justin Ellingwood

翻譯:diradw

如何在Linux VPS上定製自己的Bash Prompt

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.