The PS1 in this example displays the following three information in the prompt:
-bash-3.2$ export PS1="\u@\h \w> " ramesh@dev-db ~> cd /etc/mail ramesh@dev-db /etc/mail>
In the PS1 environment variable, you can directly execute any Linux command, by specifying in the format $(linux_command). In the following example, the command $(date) is executed to display the current time inside the prompt.
ramesh@dev-db ~> export PS1="\u@\h [\$(date +%k:%M:%S)]> " ramesh@dev-db [11:09:56]>
You can also use \t to display the current time in the hh:mm:ss format as shown below:
ramesh@dev-db ~> export PS1="\u@\h [\t]> " ramesh@dev-db [12:42:55]>
You can also use \@ to display the current time in 12-hour am/pm format as shown below:
ramesh@dev-db ~> export PS1="[\@] \u@\h> " [04:12 PM] ramesh@dev-db>
You can display output of any Linux command in the prompt. The following example displays three items separated by | (pipe) in the command prompt:
ramesh@dev-db ~> kernel_version=$(uname -r) ramesh@dev-db ~> export PS1="\!|\h|$kernel_version|\$?> " 473|dev-db|2.6.25-14.fc9.i686|0>
Display prompt in blue color, along with username, host and current directory information
$ export PS1="\e[0;34m\u@\h \w> \e[m" [Note: This is for light blue prompt] $ export PS1="\e[1;34m\u@\h \w> \e[m" [Note: This is for dark blue prompt]
Color Code Table:
Black 0;30
Blue 0;34
Green 0;32
Cyan 0;36
Red 0;31
Purple 0;35
Brown 0;33 [Note: Replace 0 with 1 for dark color]
Make the color change permanent by adding the following lines to .bash_profile or .bashrc
STARTCOLOR='\e[0;34m'; ENDCOLOR="\e[0m" export PS1="$STARTCOLOR\u@\h \w> $ENDCOLOR"
Change the background color by specifying \e[{code}m in the PS1 prompt as shown below.
$ export PS1="\e[47m\u@\h \w> \e[m"[Note: This is for Light Gray background]
Combination of background and foreground
export PS1="\e[0;34m\e[47m\u@\h \w> \e[m"[Note: This is for Light Blue foreground and Light Gray background]
Add the following to the .bash_profile or .bashrc to make the above background and foreground color permanent.
STARTFGCOLOR='\e[0;34m'; STARTBGCOLOR="\e[47m" ENDCOLOR="\e[0m" export PS1="$STARTFGCOLOR$STARTBGCOLOR\u@\h \w> $ENDCOLOR"
Play around by using the following background color and choose the one that suites your taste:
You can also display multiple colors in the same prompt. Add the following function to .bash_profile
function prompt { local BLUE="\[\033[0;34m\]" local DARK_BLUE="\[\033[1;34m\]" local RED="\[\033[0;31m\]" local DARK_RED="\[\033[1;31m\]" local NO_COLOR="\[\033[0m\]" case $TERM in xterm*|rxvt*) TITLEBAR='\[\033]0;\u@\h:\w\007\]' ;; *) TITLEBAR="" ;; esac PS1="\u@\h [\t]> " PS1="${TITLEBAR}\ $BLUE\u@\h $RED[\t]>$NO_COLOR " PS2='continue-> ' PS4='$0.$LINENO+ ' }
You can re-login for the changes to take effect or source the .bash_profile as shown below.
$. ./.bash_profile
$ prompt ramesh@dev-db [13:02:13]>
You can also change color of the PS1 prompt using tput as shown below:
$ export PS1="\[$(tput bold)$(tput setb 4)$(tput setaf 7)\]\u@\h:\w $ \[$(tput sgr0)\]"
tput Color Capabilities:
tput Text Mode Capabilities:
Color Code for tput:
Use the following codes and create your own personal PS1 Linux prompt that is functional and suites your taste. Which code from this list will be very helpful for daily use? Leave your comment and let me know what PS1 code you've used for your Linux prompt.
You can also invoke a bash shell function in the PS1 as shown below.
ramesh@dev-db ~> function httpdcount {
> ps aux | grep httpd | grep -v grep | wc -l
> }
ramesh@dev-db ~> export PS1="\u@\h [`httpdcount`]> "
ramesh@dev-db [12]> [Note: This displays the total number of running httpd processes]
You can add the following line to .bash_profile or .bashrc to make this change permanent:
function httpdcount { ps aux | grep httpd | grep -v grep | wc -l } export PS1='\u@\h [`httpdcount`]> '
You can also invoke a shell script inside the PS1 variable. In the example below, the ~/bin/totalfilesize.sh, which calculates the total filesize of the current directory, is invoked inside the PS1 variable.
ramesh@dev-db ~> cat ~/bin/totalfilesize.sh
for filesize in $(ls -l . | grep "^-" | awk '{print $5}')
do
let totalsize=$totalsize+$filesize
done
echo -n "$totalsize"
ramesh@dev-db ~> export PATH=$PATH:~/bin
ramesh@dev-db ~> export PS1="\u@\h [\$(totalfilesize.sh) bytes]> "
ramesh@dev-db [534 bytes]> cd /etc/mail
ramesh@dev-db [167997 bytes]>[Note: This executes the totalfilesize.sh to display the total file size of the current directory in the PS1 prompt]
How much customization have you done to your PS1? Can your PS1 beat Angelina Jolie?Leave a comment and share your PS1 prompt value.
Bash Cookbook, by Carl Albing, JP Vossen and Cameron Newham. Bash is a very powerful shell. This book will help you to master the bash shell and become highly productive. Whether you are a sysadmin, DBA or a developer, you have to write shell script at some point. A wise sysadmin knows that once you’ve mastered the shell-scripting techniques, you can put your servers on auto-pilot mode by letting the shell-scripts do the grunt work. To get to the auto-pilot mode of sysadmin, you definitely need to master the examples provided in this cookbook. There are quiet few Bash shell books out there. But, this books tops them all by giving lot of detailed examples.