User Tools

Site Tools


linux:linux_bash_prompt

Change / Setup bash custom prompt (PS1)

History commands

Add history with a timestamp and increase history commands size, for example in /etc/bashrc or ~root/.bash_profile:

export HISTSIZE=10000
export HISTTIMEFORMAT="%F %T   "

Prompt

http://www.ibm.com/developerworks/linux/library/l-tip-prompt/

http://www.cyberciti.biz/tips/howto-linux-unix-bash-shell-setup-prompt.html

http://www.understudy.net/custom.html

So when executing interactively, bash displays the primary prompt PS1 when it is ready to read a command, and the secondary prompt PS2 when it needs more input to complete a command. Bash allows these prompt strings to be customized by inserting a number of backslash-escaped special characters that are decoded as follows:

SequenceDescription
\aThe ASCII bell character (you can also type \007)
\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 (you can also type \033)
\hFirst part of hostname (such as “mybox”)
\HFull hostname (such as “mybox.mydomain.com”)
\jThe number of processes you've suspended in this shell by hitting CTRL+Z
\lthe basename of the shell’s terminal device name (such as “ttyp4”)
\nnewline
\rcarriage return
\sThe name of the shell executable (such as “bash”)
\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.04)
\VBash version, including patchlevel
\wthe current working directory, with $HOME abbreviated with a tilde
\Wthe basename of the current working directory, with $HOME abbreviated with a tilde
\!Current command's position in the history buffer
\#Command number (this will count up at each prompt, as long as you type something)
\$if the effective UID is 0, a #, otherwise a $
\nnnCommand number (this will count up at each prompt, as long as you type something)
\\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

A list of handy tput command line options

  tput bold - Bold effect
  tput rev - Display inverse colors
  tput sgr0 - Reset everything
  tput setaf {CODE}- Set foreground color, see color {CODE} table below for more information.
  tput setab {CODE}- Set background color, see color {CODE} table below for more information.

Various color codes for the tput command

Color {code}Color
0Black
1Red
2Green
3Yellow
4Blue
5Magenta
6Cyan
7White

Let us try to set the prompt so that it can display today’d date and hostname:

PS1="\d \h $ "

Output:

Sat Jun 02 server $ 

Now setup prompt to display date/time, hostname and current directory:

$ PS1="[\d \t \u@\h:\w ] $ "

Output:

[Sat Jun 02 14:24:12 vivek@server:~ ] $

Append the code as follows

# If id command returns zero, you’ve root access.
if [ $(id -u) -eq 0 ];
then # you are root, set red colour prompt
  PS1="\\[$(tput setaf 1)\\]\\u@\\h:\\w #\\[$(tput sgr0)\\]"
else # normal
  PS1="[\\u@\\h:\\w] $"
fi

Other samples:

export PS1='\[33[0;32m\][\u@\h \W]\$\[33[0m\] '
export PS1="\[\e[0;31m\][\u@\h \w]\$ \[\e[m\]"
export PS1="\e[0;31m[\u@\h \W]\$ \e[m "
export PS1=’\[\e[0;32m\]\u@\h:\w\$ \[\e[0m\]’
export PS1="\[$(tput setaf 1)\]\u@\h:\w $ \[$(tput sgr0)\]"
linux/linux_bash_prompt.txt · Last modified: 2021/01/01 21:25 (external edit)