首頁 繪圖設計 工作閒聊 比賽活動 美術討論 標籤 圖片
Bash Prompt Example: Color/User/Date/Time/Host/Path
Type(Type) 2014/3/6 14:42 (Since 2014/3/3 13:23)

Bash Prompt Example: Color / User / Date / Time / Host / Path




1. User => \u
代碼:

\u


2. Color
代碼:

BOLD => \[\033[1m\]

RED => \[\033[31m\]

BOLD READ => \[\033[1;31m\]

GREEN => \[\033[32m\]

BOLD GREEN => \[\033[1;32m\]

YELLOW => \[\033[33m\]

BOLD YELLOW => \[\033[1;33m\]

BLUE => \[\033[34m\]

BOLD BLUE => \[\033[1;34m\]

OFF => \[\033[m\] or \[\e[0m\]


3. Host => \h
e.g. username@hostname prmpt
代碼:

\u@\h


4. Date (No Year) => \d
e.g. Mon Mar 03
代碼:

\d


5. Date (YYYY-MM-DD) => \D{Format}
e.g. 2012-08-15
代碼:

\D{%Y-%m-%d}


6. Unix Time Stamp => \D{%s}
e.g. 1365656566
代碼:

\D{%s}


7. Absolute Path => \w
e.g. ~username/myfolder/
代碼:

\w



8. New Lines => \n
e.g.
代碼:

\n



9. Related Path => \W
代碼:

\W



10. Time => \A \t
代碼:

\A     => 14:31      (24 HH:II)
\t     => 14:31:21  (24 HH:II:SS)
\T   =>  02:31:21  (12 HH:II Without AM/PM)
\@   =>  02:31 PM  (AM/PM will be translated up to your locale)



11. History Count => \#
代碼:

\#  ==> It will show how many commands in this shell (bash history counting)






Some bash prompt examples



Example #1: 'user@host $ '
代碼:

export PS1='\u@\h $ '


Example #2: Two lines with (1) First line Date Time (2) 2nd Line '>> ' prompt
代碼:

export PS1='\n\u@\h \[\d \t\] \w \n>> '


Example #3: BOLD + Colorful upon Example #2
代碼:

export PS1='\n\[\033[1;32m\]\u@\h \t \[\e[33m\]\w\[\e[0m\]\n>> '



See also: http://xor.tw/4zlm2


Type(Type) 2014/3/3 13:23


See also
=> https://wiki.archlinux.org/index.php/Color_Bash_Prompt

Hate the "~" of \w? (Want the HOME directory to be absolute path as well!)
=> superuser.com/questions/202212/how-can-i-display-the-absolute-path-in-bash-prompt



Details PS1/PS2 Options


代碼:

              \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 vari‐
                     able)
              \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


(1,313 views)
[更多討論] 討論區 Windows, Linux, Perl, PHP, C/C++, Driver, Web 理論、應用、硬體、軟體




"Bash Prompt Example: Color/User/Date/Time/Host/Path" 傳統頁面(電腦版)

首頁 繪圖設計 工作閒聊 比賽活動 美術討論 標籤 圖片
傳統桌面版 [ 登入/註冊 ]
© Vovo2000.com Mobile Version 小哈手機版 2018