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
share|improve this question

closed as not constructive by Josh Caswell, Luchian Grigore, g.d.d.c, casperOne Dec 12 '11 at 3:49

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. If this question can be reworded to fit the rules in the help center, please edit the question.

1  
A person using the tautology "may or may not" is prompting me to bash them ;) – Dan Sep 19 '08 at 19:58
    
The question is useful, it just shouldn't be merged with the answer. – dhill Feb 10 '12 at 14:05
11  
I wonder about SO sometimes. A question with 40K views, 72 upvotes, 50 favorites, whose accepted answer has 58 upvotes, and the top google score for "BASH best PS1", has been closed as "Not Constructive"...yeah, ok then. (Note, I know SO's rules, but rules begat exceptions, don't they then?) – Cody S Jan 12 '15 at 22:30
    
Yes. A problem everyone has, everyone looks for an answer, there exists a range of good answers, and becomes top Google. Not constructive – Charles Merriam Apr 29 '17 at 17:43

14 Answers 14

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.

share|improve this answer
    
Excellent. Exactly what I'm looking for. Could you please upload it? – demonkoryu Jan 20 '11 at 8:17

in .bashrc:

PS1='\$ '

(H/T @bitmask for the edit)

share|improve this answer
4  
always nice to see the minimalistic option mentioned. i personally couldn't stand it, but it does make you take a moment to reflect on its simplicity :) – wprl May 8 '11 at 5:14
9  
How can anyone work with not seeing the current location in his prompt? :o – ThiefMaster Nov 21 '12 at 7:23
7  
You remember or pwd – Gunchars Jan 6 '13 at 23:50
    
@BenCollins What's that \ called, in that context? I can see what it's doing (forcing evaluation of the variable each time, which is necessary if you're using PWD in the definition if you want it to change when you change directory), but it's one of those characters which is stripped from google searches. – Dr Eval Oct 16 '14 at 15:43
1  
@BenCollins On ksh, if i set PS1 to "$PWD " then the prompt is the full current directory, but if I CD elsewhere the prompt doesn't change. Setting PS1 to "\$PWD " means the prompt changes as I move around. – Dr Eval Oct 16 '14 at 16:02

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
share|improve this answer
    
Please note that on StackOverflow, "first one" depends on things such as upvote count or user-specified order, as the top post may change. – Camilo Martin Jun 18 '14 at 11:06
PS1="\n\e[30;1mUSER ( \e[0m\e[33;1m\w\e[0m\e[30;1m )\e[0m\e[33;33m\n$ \e[0m"
share|improve this answer

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 <<< "$begin"
  begin_ns="${begin_ns##+(0)}"
  # PENDING - date takes about 11ms, maybe could do better by digging in
  # /proc/$$.  
  read end_s end_ns <<< $(date +"%s %N")
  end_ns="${end_ns##+(0)}"
  local s=$((end_s - begin_s))
  local ms
  if [ "$end_ns" -ge "$begin_ns" ]
  then
    ms=$(((end_ns - begin_ns) / 1000000))
  else
    s=$((s - 1))
    ms=$(((1000000000 + end_ns - begin_ns) / 1000000))
  fi
  elapsed="$(printf " %2u:%03u" $s $ms)"
  if [ "$s" -ge 300 ]
  then
    elapsed="$elapsed [$(human_time $s)]"
  fi
}



human_time()
{
  local s=$1
  local days=$((s / (60*60*24)))
  s=$((s - days*60*60*24))
  local hours=$((s / (60*60)))
  s=$((s - hours*60*60))
  local min=$((s / 60))
  if [ "$days" != 0 ]
  then
    local day_string="${days}d "
  fi
  printf "$day_string%02d:%02d\n" $hours $min
}  

timer_prompt()
{
  status=$?
  local size=16
  calc_elapsed
  if [ "${#PWD}" -gt $size ]
  then
    pwd_string="${PWD: -$size}"
  else
    pwd_string="$(printf "%${size}s" $PWD)"
  fi
  PS1="$bold$elapsed $pwd_string $status\\$ $plain"
  begin=
}

set_begin
trap set_begin DEBUG
PROMPT_COMMAND=timer_prompt
share|improve this answer
1  
Here's a thread about displaying run times in Bash prompts: stackoverflow.com/questions/1862510/… – willdye Dec 17 '12 at 17:04

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
share|improve this answer

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

share|improve this answer

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\] "`
share|improve this answer

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]$ 
share|improve this answer

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

PS1="(\d \t) (\u@\h:\w)\nbash> "
export PS1
share|improve this answer

I like to keep it (relatively) simple:

username@hostname:/full/path>

PS1='\u@\h:\w> '
export PS1
share|improve this answer

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    
share|improve this answer

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
$ 
share|improve this answer
2  
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 Jun 20 '10 at 2:28

Not the answer you're looking for? Browse other questions tagged or ask your own question.