tags:

views:

1815

answers:

13

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
+4  A: 

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
$
jtimberman
oh, I like this!
Aeon
This is moot if you have that newline before the dollar prompt but if you want to remove that then you need to surround all the non-printing control characters with `\\[` and `\\]`. Otherwise long commands won't wrap properly.
dreeves
+7  A: 

Take a gander at this reddit thread for a large number of examples.

William Keller
you bastard, that's an hour of my time spent on tweaking my prompt now ;) That thread is awesome.
Aeon
I loved that thread when it was first posted, some great ideas there!
William Keller
This was full of great ideas, which is why I accepted it. Thanks for the tip.
glamdringlfo
A: 

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 < ${#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="[\u@\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
icco
A: 

PS1="\n\e[30;1mUSER ( \e[0m\e[33;1m\w\e[0m\e[30;1m )\e[0m\e[33;33m\n$ \e[0m"

+1  A: 

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.

Michael Johnson
+2  A: 

in .bashrc:

PS1='$ '
Ben Collins
A: 

I like to keep it (relatively) simple:

username@hostname:/full/path>

PS1='\u@\h:\w> '
export PS1
dr-jan
A: 

I've had this as my bash prompt for a long time:

PS1="(\d \t) (\u@\h:\w)\nbash> "
export PS1
jfm3
A: 

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]$ 
dsm
+5  A: 

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\] "`
Drew Stephens
A: 

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

warren
A: 

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
Colas Nahaboo
A: 

i modified the first one 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

spoonsphere