Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
How-to for customizing your bash prompt. For PyLadies tutorial on the CLI. You can also find this in a slightly more readable form on the PyLadies PDX meetup site under "Pages".
Customize Your Bash Prompt
Your bash prompt comes with a standard command prompt with a default setting something like “PS1="\u@\h \w>” in your .bash_profile or .profile (if you are using OSX) that translates to something like this:
UserName YourComputerName DirectoryName $
While useful, this is kind of boring and doesn’t give us very much information about the environment we’re working in. So let’s customize our prompts!
Open your Terminal and type in ~/.bash_profile** (see link at end of this doc for discussion of .bash_profile v. .bashrc). This should open your bash_profile file in TextEdit if you are on a Mac. Alternatively, you can open with another editor by typing in “vi ~/.bash_profile”, substituting “vi” for your chosen editor (unless your chosen editor is vi!).
If you can’t find these files or would like to search for them visually, you can reveal your hidden dot files using:
$ defaults write AppleShowAllFiles YES
After pressing enter, navigate to your finder icon on your launch bar. Press the Alt (Option) key and Ctrl-click on the Finder icon. Choose “Relaunch” from the menu, then release keys. This will relaunch the finder with your hidden files revealed.
When you are finished accessing your hidden files, be sure to hide them again for safety by repeating the process, only this time typing into the CLI:
$ defaults write AppleShowAllFiles NO
((**Note: if using pre-Lion OSX, use TRUE and FALSE instead of YES and NO.))
You can also check your current PS1 settings in the CLI by typing in: $ echo $PS1
And change your PS1 variable in the CLI by typing in: PS1=’.....’ (though you won’t be able to add Chris’s ‘parse_git_branch’ function this way).
Add this template (below, between lines) to the bottom of your bash_profile:
Note: This will produce a command prompt that looks like this (using mine as an example:
My-MacBook-Air Wed Feb 20 19:51 TwitterAPI (master) ♡ $
….and if I press <enter> and command returns false, then:
My-MacBook-Air Wed Feb 20 19:31 TwitterAPI (master) ♥ $
# Prompt customizations:
# This function lets us know which branch we are on when working in a local repo:
function parse_git_branch() {
x=`git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'`
if [[ "$x" == "" ]]; then
echo ""
echo "$x "
# Checks status to see whether last command returned a true or false:
export PROMPT_COMMAND='export OLD_STATUS=$?'
# Build your new command prompt:
export PS1='\[\033[1;35m\]\h \[\033[0;30m\]\d \[\033[0;30m\]\A \[\033[1;30m\]\W \[\033[0;30m\]`parse_git_branch` \[\033[0;30m\]`if [ $OLD_STATUS = 0 ]; then echo \♡; else echo \♥; fi` \[\033[1;33m\]\$ \[\033[0m\]'
So let’s break that down.
* “export PS1=”: means export the PS1 variable to primary prompt on bash load
* “[\033[1;35m\]\h “: everything in between the brackets is setting the color of the following variable, in this case the “\h “ for hostname.
* The \033 doesn’t change--it just tells bash that it can expect 33-octal ASCI color codes to follow.
* The 1;35m\ is the actual color code. For a list of these codes refer to chart below.
* The \h, as we said before, is the variable name for the piece of data that you want your command to display. Be sure to add a space after the variable and before the next term in your PS1 ‘sentence’ (beginning with a “\”) or your prompt will look like this MyComputerFeb4Desktop$, etc.
Use the following variables, color codes, and symbols to customize the given template (substituting your choices in for the ones given). Note that you can use the “\n ” newline variable to break your prompt into multiple lines.
Bash variables:
\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
Bash Color Codes:
Black 0;30m Dark Gray 1;30m
Blue 0;34m Light Blue 1;34m
Green 0;32m Light Green 1;32m
Cyan 0;36m Light Cyan 1;36m
Red 0;31m Light Red 1;31m
Purple 0;35m Light Purple 1;35m
Brown 0;33m Yellow 1;33m
Light Gray 0;37m White 1;37m
Symbols (just copy/paste symbol itself rather than inserting UTF encoding):
☀ Sun
☼ Sun w/Rays
☁ Cloud
☽ Moon
☆ White Star
★ Black Star
☠ Skull and Crossbones
⚑ Black Flag
⚐ White Flag
☭ Hammer and Sickle
☺ Smiley Face
☹ Frowny Face
♡ White Heart
♥ Black Heart
♨ Hot Springs (or Hot Soup, as I like to think of it)
💔 Broken Heart (copy/paste square--will show up in bash)
😱 Screaming Face (copy/paste square--will show up in bash)
🎂 Birthday Cake (copy/paste square--will show up in bash)
⛤ Pentagram (copy/paste square--will show up in bash)
…...for more see (copy/paste symbol)
4. If you want, you can also customize the secodary (PS2), tertiary (PS3), and quaternary (PS4) prompts, which may be issued when programming in your shell.
PS2: Is the continuation interactive prompt, and is issued when bash needs more information to complete your command/script (ie, when you are issuing a multi-line command). The default value is “>”, but you might want it to say “more ->” or “continue ->”, etc. for clarity.
PS3: The tertiary prompt string is called when you issue the select command inside of a shell script (ie, when user is given a list of selections from which to pick). By default your prompt will ask you for “#?”, but you might want it to ask you for an option more explicitly. See for explaination and easy-to-follow example.
PS4: The quaternary string is used to prefix tracing output (to STDOUT) when invoking “set -x” for debugging (ie, your program will print out each line of the script as it is running commands). By default, your prompt will give you a “++” for every line, but you might want to set it to give you line numbers instead using: export PS$=’$LINENO+ ‘.
PROMPT_COMMAND: Bash executes the PROMPT_COMMAND just before displaying the PS1 variable. You can use this to:
set window title: (,
make substitutions in your PS1 (though setting PS1 directly is recommended): (,
or control history across multiple Terminal sessions: (
5. Read more:
...and, if you are confused about .bash_profile vs .bashrc (since many sites will tell you to use one or the other, which can be confusing if you don’t understand the difference. Additionally, OSX doesn’t come with a .bashrc file, which, although you can easily make your own, doesn’t make anything less confusing) might take a look at:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.