Ana Sayfa / Linux / Bash Script / Customizing PS1

Customizing PS1

Colorize and customize terminal prompt

This is how the author sets their personal PS1 variable:

 gitps1=$(git branch 2>/dev/null | grep '*')
 gitps1="${gitps1:+ (${gitps1/#\* /})}"
 echo "$gitps1"
#Please use the below function if you are a mac user
 git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
 echo "$(date +%r)"
if [ "$color_prompt" = yes ]; then
 if [ x$EUID = x0 ]; then
\[\033[1;34m\]\w\[\033[00m\]\[\033[1;36m\]$(gitPS1)\[\033[00m\] \[\033[1;31m\]:/#\[\033[00m\] '
\[\033[1;34m\]\w\[\033[00m\]\[\033[1;36m\]$(gitPS1)\[\033[00m\] \[\033[1;32m\]:/$\[\033[00m\] '
 PS1='[$(timeNow)] \u@\h \w$(gitPS1) :/$ '

And this is how my prompt looks like:

Color reference:

# Colors
txtblk='\e[0;30m' # Black - Regular
txtred='\e[0;31m' # Red
txtgrn='\e[0;32m' # Green
txtylw='\e[0;33m' # Yellow
txtblu='\e[0;34m' # Blue
txtpur='\e[0;35m' # Purple
txtcyn='\e[0;36m' # Cyan
txtwht='\e[0;37m' # White
bldblk='\e[1;30m' # Black - Bold
bldred='\e[1;31m' # Red
bldgrn='\e[1;32m' # Green
bldylw='\e[1;33m' # Yellow
bldblu='\e[1;34m' # Blue
bldpur='\e[1;35m' # Purple
bldcyn='\e[1;36m' # Cyan
Bash Notes for Professionals 105
bldwht='\e[1;37m' # White
unkblk='\e[4;30m' # Black - Underline
undred='\e[4;31m' # Red
undgrn='\e[4;32m' # Green
undylw='\e[4;33m' # Yellow
undblu='\e[4;34m' # Blue
undpur='\e[4;35m' # Purple
undcyn='\e[4;36m' # Cyan
undwht='\e[4;37m' # White
bakblk='\e[40m' # Black - Background
bakred='\e[41m' # Red
badgrn='\e[42m' # Green
bakylw='\e[43m' # Yellow
bakblu='\e[44m' # Blue
bakpur='\e[45m' # Purple
bakcyn='\e[46m' # Cyan
bakwht='\e[47m' # White
txtrst='\e[0m' # Text Reset


  • Make the changes in ~/.bashrc or /etc/bashrc or ~/.bash_profile or ~./profile file (depending on the OS) and save it.
  • For root you might also need to edit the /etc/bash.bashrc or /root/.bashrc file
  • Run source ~/.bashrc (distro specific) after saving the file.
  • Note: if you have saved the changes in ~/.bashrc, then remember to add source ~/.bashrc in your ~/.bash_profile so that this change in PS1 will be recorded every time the Terminal application starts.

Show git branch name in terminal prompt

You can have functions in the PS1 variable, just make sure to single quote it or use escape for special chars:

 gitps1=$(git branch 2>/dev/null | grep '*')
 gitps1="${gitps1:+ (${gitps1/#\* /})}"
 echo "$gitps1"
PS1='\u@\h:\w$(gitPS1)$ '

It will give you a prompt like this:

user@Host:/path (master)$


  • Make the changes in ~/.bashrc or /etc/bashrc or ~/.bash_profile or ~./profile file (depending on the OS) and save it.
  • Run source ~/.bashrc (distro specific) after saving the file.

Show time in terminal prompt

 echo "$(date +%r)"
PS1='[$(timeNow)] \u@\h:\w$ '

It will give you a prompt like this:

[05:34:37 PM] user@Host:/path$


  • Make the changes in ~/.bashrc or /etc/bashrc or ~/.bash_profile or ~./profile file (depending on the OS) and save it.
  • Run source ~/.bashrc (distro specific) after saving the file.

Show a git branch using PROMPT_COMMAND

If you are inside a folder of a git repository it might be nice to show the current branch you are on. In ~/.bashrc or
/etc/bashrc add the following (git is required for this to work):

function prompt_command {
 # Check if we are inside a git repository
 if git status > /dev/null 2>&1; then
 # Only get the name of the branch
 export GIT_STATUS=$(git status | grep 'On branch' | cut -b 10-)
 export GIT_STATUS=""
# This function gets called every time PS1 is shown
PS1="\$GIT_STATUS \u@\h:\w\$ "

If we are in a folder inside a git repository this will output:

branch user@machine:~$

And if we are inside a normal folder:


Change PS1 prompt

To change PS1, you just have to change the value of PS1 shell variable. The value can be set in ~/.bashrc or
/etc/bashrc file, depending on the distro. PS1 can be changed to any plain text like:

PS1="hello "

Besides the plain text, a number of backslash-escaped special characters are supported:

\aan ASCII bell character (07)
\ dthe 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
\ ean ASCII escape character (033)
\ hthe hostname up to the first ‘.’
\ Hthe hostname
\ Jthe number of jobs currently managed by the shell
\ lthe basename of the shell’s terminal device name
\ nnewline
\ rcarriage return
\ sthe name of the shell, the basename of $0 (the portion following the final slash)
\ tthe current time in 24-hour HH:MM:SS format
\ Tthe current time in 12-hour HH:MM:SS format
\ @ the current time in 12-hour am/pm format
\ Athe current time in 24-hour HH:MM format
\ uthe username of the current user
\ vthe version of bash (e.g., 2.00)
\ Vthe release of bash, version + patch level (e.g., 2.00.0)
\ wthe current working directory, with $HOME abbreviated with a tilde
\ Wthe 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

So for example, we can set PS1 to:

PS1="\u@\h:\w\$ "

And it will output:


Show previous command return status and time

Sometimes we need a visual hint to indicate the return status of previous command. The following snippet makeput it at the head of the PS1.

Note that the __stat() function should be called every time a new PS1 is generated, or else it would stick to the return status of last command of your .bashrc or bash_profile.


function __stat() {
 if [ $? -eq 0 ]; then
 echo -en "$Green ? $Color_Off "
 echo -en "$Red ? $Color_Off "
PS1+="[\t] "
PS1+="\e[0;33m\u@\h\e[0m:\e[1;34m\w\e[0m \n$ "

export PS1

Bunada Göz Atın

PHP Types

Type Comparison There are two types of comparison: loose comparison with == and strict comparison …

Bir yanıt yazın

E-posta adresiniz yayınlanmayacak. Gerekli alanlar * ile işaretlenmişlerdir