可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
What are some elements in your favorite Bash prompt?
I like to have an indicator of the success of the most recent command, like so (in .bashrc
):
function exitstatus { EXITSTATUS="$?" BOLD="\[\033[1m\]" RED="\[\033[1;31m\]" GREEN="\[\e[32;1m\]" BLUE="\[\e[34;1m\]" OFF="\[\033[m\]" PROMPT="[\u@\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
回答1:
Take a gander at "Bash users: What do you have for your $PS1?" for a large number of examples.
回答2:
in .bashrc:
PS1='\$ '
(H/T @bitmask for the edit)
回答3:
Since I hop between a number of different machines all day, my dev workstation, dev servers and staging environments, I've taken coloring the machine name in my prompts with the following snippet to give a visual indicator when I look at the sea of terms on my monitors what machines they are connected to:
# 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
Then, the full PS1 variable with the colored hostname, colon (red if root, i.e. sudo -s) and full path:
PS1=`echo -ne "$HOST_COLOR\h\[\033[00m\]\[\e[$COLON_COLOR\]:\[\033[01;32m\]\w\[\033[00m\]\\[\033[01;33m\]\$\[\033[00m\] "`
回答4:
Here's mine.
# 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\$
Looks like this 'laptop' is green and the rest is default terminal foreground, of course.
laptop:~/bin $
回答5:
I like the prompt to tell roughly how long previous command took to execute. Like this:
0:007 /home/jcl 0$ sleep 1
1:012 /home/jcl 0$
It can be implemented like this:
bold='\[\e[1m\]' plain='\[\e[0m\]' set_begin() { if [ -z "$begin" ] then begin="$(date +"%s %N")" fi } calc_elapsed() { read begin_s begin_ns
回答6:
My prompt:
[Fri Sep 19 10:33 AM]
[nat@Forge ~]$
#################################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
回答7:
I've had this as my bash prompt for a long time:
PS1="(\d \t) (\u@\h:\w)\nbash> " export PS1
回答8:
I use a custom version of Glandium.org's "Adding some VCS information in bash prompt" prompt. The biggest thing I've done pulled out the VCS collection bits into a separate script.
This prompt is great because it gives me a lot of information I need to know about the current project I'm working on, based in VCS information. I've also customized mine to show command return results, Screen window number, user and machine names, and the current time. It's pretty long (42 characters or more) but I use wide terminals, so it's not a big deal.
For instance, my current prompt (after a bit of editing) looks like:
[0-1]michael@hal(g:proj1[topic/fix-123]app)[10:50]|$
Which means last command had a result of 0, in screen window 1, as michael logged into hal. Current directory is a Git working directory for proj1, inside branch topic/fix-123 in directory app at 10:50 am. Logged in as a regular user.
BTW, if anyone would like my customized script, please let me know... I'll be happy to put it either here or somewhere else easily accessible.
回答9:
I modified the original poster's example a bit:
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:
I like to keep it (relatively) simple:
username@hostname:/full/path>
PS1='\u@\h:\w> ' export PS1
回答11:
mine:
function prompt_err { if test "$?" -eq 0; then PS1=': \W$; '; else PS1=': \W [ERROR#$?]$; '; fi } PROMPT_COMMAND=prompt_err PS2=' '
Does:
- show the directory name
- allow to copy/paste the whole line into another shell to re-execute the same command, ignoring the prompt
- if the previous command exit status was not 0, print a visible ERROR with status code
- secondary prompt are spaces to allow copy/paste
回答12:
I like this:
\[\033[33m\][\u@\[\033[1;31m\]\h]\]\033[0m {\W}\n\033[1;34m\]\w\]\033[0m >
it puts
[name]@[hostname] [pwd] >
with [name]
in yellow, [hostname]
in red and [pwd]
in blue
回答13:
PS1="\n\e[30;1mUSER ( \e[0m\e[33;1m\w\e[0m\e[30;1m )\e[0m\e[33;33m\n$ \e[0m"
回答14:
Mine, at home, is pretty simple:
[user@machinename:/full/CWD]$
At work, we have a concept of 'environments' for the production and test users, and the prompt is
(env;sub-env) [user@machinename:/full/CWD]$