linux PS1提示符

來源:互聯網
上載者:User

# 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======================================================

\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時,提示符為:# ,普通使用者則為:$    CentOS中關於這些:

\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

\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

    於是可以根據這些指定自己喜歡的命令提示字元格式:PS1="\u@\h:\w\$ "

    但是這樣設定之後顯示的提示符是沒有顏色的,如果希望加入顏色設定,可以這樣:PS1="\e[1;32m\u\e[m\e[1;33m@\e[m\e[1;35m\h\e[m:\w\$ "

    此時,在終端中的顯示應該是彩色的提示符了,其中的\e[1;xxm和\e[m就是改變終端字型顏色的部分,前者是修改顏色,後者是恢複到正常顯示。但是這時還會有問題,在提示符中輸入第一行時,斷行符號換行不正常,導致顯示很混亂,因此,除了這個修改之外,還需要將全部非列印字元用專用的 bash 逸出序列 "\[" 和 "\]" 括起來。這兩個序列通知 bash,被括起來的字元不佔用行上的任何空間,這樣就使自動換行能夠繼續正常工作。最終版本如下(可解決帶顏色提示符的不換行問題):PS1="\[\e[1;32m\]\u\[\e[m\]\[\e[1;33m\]@\[\e[m\]\[\e[1;35m\]\h\[\e[m\]:\w\$ "
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.