Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
TetraWiki
Search
Search
Appearance
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Linux 101 hacks
(section)
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Special pages
Page information
Appearance
move to sidebar
hide
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
='''Chapter 6: Colorful and Functional Shell'''= '''Prompt Using PS1''' =='''Hack 34. Display username, hostname and basename of directory in the prompt'''== The PS1 in this example displays following three information in the prompt: o \u β Username o \h β Hostname o \W β Base name of the current working directory -bash-3.2$ '''export PS1="\u@\h \W> "''' ramesh@dev-db ~> cd /etc/mail ramesh@dev-db mail> =='''Hack 35. Display current time in the prompt'''== 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 <nowiki>[\$(date</nowiki>''' '''+%k:%m:%S)]> "''' ramesh@dev-db <nowiki>[11:09:56]></nowiki> 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 <nowiki>[\t]> </nowiki>"''' ramesh@dev-db <nowiki>[12:42:55]></nowiki> You can also use \@ to display the current time in 12-hour am/pm format as shown below: ramesh@dev-db ~> '''export <nowiki>PS1="[\@] </nowiki>\u@\h> "''' <nowiki>[04:12 </nowiki>PM] ramesh@dev-db> =='''Hack 36. Display output of any command in the prompt'''== You can display output of any Linux command in the prompt. The following example displays three items separated by | (pipe) in the command prompt: o \!: The history number of the command o \h: hostname # $kernel_version: The output of the uname -r command from $kernel_version variable o \$?: Status of the last command 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> =='''Hack 37. Change foreground color of the prompt'''== Display prompt in blue color, along with username, host and current directory information $ '''export <nowiki>PS1="\e[0;34m\u@\h </nowiki>\w> <nowiki>\e[m </nowiki>"''' <nowiki>[</nowiki>'''Note''': This is for light blue prompt] $ '''export <nowiki>PS1="\e[1;34m\u@\h </nowiki>\w> <nowiki>\e[m </nowiki>"''' <nowiki>[</nowiki>'''Note''': This is for dark blue prompt] o <nowiki>\e[ - Indicates the beginning of color prompt</nowiki> # x;ym - Indicates color code. Use the color code values mentioned below. o <nowiki>\e[m - indicates the end of color prompt</nowiki> '''Color Code Table:''' Black 0;30 Blue 0;34 Green 0;32 Cyan 0;36 Red 0;31 Purple 0;35 Brown 0;33 <nowiki>[</nowiki>'''Note''': Replace 0 with 1 for dark color] Make the color change permanent by adding the following lines your ~/.bash_profile or ~/.bashrc $ vi ~/.bash_profile <nowiki>STARTCOLOR='\e[0;34m'; ENDCOLOR="\e[0m"</nowiki> export PS1="$STARTCOLOR\u@\h \w> $ENDCOLOR" =='''Hack 38. Change background color of the prompt'''== <nowiki>Change the background color by specifying \e[{code}m in the PS1 prompt as shown below.</nowiki> $ '''export <nowiki>PS1="\e[47m\u@\h </nowiki>\w> <nowiki>\e[m </nowiki>"''' <nowiki>[</nowiki>'''Note''': This is for Light Gray background] Combination of background and foreground. $ '''export <nowiki>PS1="\e[0;34m\e[47m\u@\h </nowiki>\w> <nowiki>\e[m </nowiki>"''' <nowiki>[</nowiki>'''Note''': This is for Light Blue foreground and Light Gray background] Add the following to your ~/.bash_profile or ~/.bashrc to make the above background and foreground color permanent. <nowiki>$ vi ~/.bash_profile STARTFGCOLOR='\e[0;34m'; STARTBGCOLOR="\e[47m" ENDCOLOR="\e[0m"</nowiki> export PS1="$STARTFGCOLOR$STARTBGCOLOR\u@\h \w> $ENDCOLOR" Play around by using the following background color and choose the one that match your taste: o <nowiki>\e[40m</nowiki> o <nowiki>\e[41m</nowiki> o <nowiki>\e[42m</nowiki> o <nowiki>\e[43m</nowiki> o <nowiki>\e[44m</nowiki> o <nowiki>\e[45m</nowiki> o <nowiki>\e[46m</nowiki> o <nowiki>\e[47m</nowiki> =='''Hack 39. Display multiple colors in the prompt'''== You can also display multiple colors in the same prompt. Add the following function to your ~/.bash_profile function prompt { <nowiki>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</nowiki> <nowiki>xterm*|rxvt*) TITLEBAR=β\[\033]0;\u@\h:\w\007\]β</nowiki> <nowiki>;;</nowiki> <nowiki>*) TITLEBAR=β"</nowiki> <nowiki>;;</nowiki> esac <nowiki>PS1=β\u@\h [\t]> β PS1=β${TITLEBAR}\</nowiki> <nowiki>$BLUE\u@\h $RED[\t]>$NO_COLOR β PS2=βcontinue-> β PS4=β$0.$LINENO+ β</nowiki> } You can re-login for the changes to take effect or source the .bash_profile as shown below. $. ./.bash_profile $ '''prompt''' ramesh@dev-db <nowiki>[13:02:13]></nowiki> =='''Hack 40. Change the prompt color using tput'''== You can also change color of the PS1 prompt using tput as shown below: <nowiki>$ export PS1="\[$(tput bold)$(tput setb 4)$(tput setaf 7)\]\u@\h:\w $ \[$(tput sgr0)\]β</nowiki> tput Color Capabilities: o <nowiki>tput setab [1-7] - Set a background color using ANSI escape</nowiki> o <nowiki>tput setb [1-7] - Set a background color</nowiki> o <nowiki>tput setaf [1-7] - Set a foreground color using ANSI escape</nowiki> o <nowiki>tput setf [1-7] - Set a foreground color</nowiki> tput Text Mode Capabilities: o tput bold - Set bold mode o tput dim - turn on half-bright mode o tput smul - begin underline mode o tput rmul - exit underline mode o tput rev - Turn on reverse mode58 {| style="border-spacing:0;" | colspan="3" style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| '''Linux 101 Hacks''' | colspan="0" style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| www.thegeekstuff.com |- | colspan="3" style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| o tput smso - Enter standout mode (bold on rxvt) |- | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| o | colspan="2" style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| tput rmso - Exit standout mode | colspan="0" style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| |- | colspan="3" style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| o tput sgr0 - Turn off all attributes | colspan="0" style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| |- | colspan="3" style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Color Code for tput: | colspan="0" style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| |- | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| o | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0 | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| β Black | colspan="0" style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| |- | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| o | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 1 | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| β Red | colspan="0" style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| |- | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| o | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 2 | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| β Green | colspan="0" style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| |- | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| o | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 3 | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| β Yellow | colspan="0" style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| |- | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| o | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 4 | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| β Blue | colspan="0" style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| |- | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| o | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 5 | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| β Magenta | colspan="0" style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| |- | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| o | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 6 | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| β Cyan | colspan="0" style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| |- | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| o | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 7 | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| - White | colspan="0" style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| |} =='''Hack 41. Create your own prompt using the available codes for PS1 variable'''== Use the following codes and create your own personal PS1 Linux prompt that is functional and suites your taste. o \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 o \e an ASCII escape character (033)59 {| style="border-spacing:0;" | colspan="2" style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| '''Linux 101 Hacks''' | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| www.thegeekstuff.com |- | colspan="2" style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| o \h the hostname up to the first part | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| |- | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| o | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| \H the hostname | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| |- | colspan="3" style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| o \j the number of jobs currently managed by the shell |- | colspan="3" style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| o \l the basename of the shellβs terminal device name |- | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| o | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| \n newline | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| |- | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| o | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| \r carriage return | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| |} # \s the name of the shell, the basename of $0 (the portion following the final slash) o \t the current time in 24-hour HH:MM:SS format o \T the current time in 12-hour HH:MM:SS format o \@ the current time in 12-hour am/pm format o \A the current time in 24-hour HH:MM format o \u the username of the current user o \v the version of bash (e.g., 2.00) o \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 o \! the history number of this command o \# the command number of this command o \$ if the effective UID is 0, a #, otherwise a $ o \nnn the character corresponding to the octal number nnn o \\ a backslash # <nowiki>\[ begin a sequence of non-printing characters, which could be used to embed a terminal control sequence into the prompt </nowiki> o \] end a sequence of non-printing character =='''Hack 42. Use bash shell function inside PS1 variable'''== 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 <nowiki>[`httpdcount`]> </nowiki>"''' ramesh@dev-db <nowiki>[12]></nowiki> <nowiki>[</nowiki>'''Note''': This displays the total number of running httpd processes] You can add the following line to your ~/.bash_profile or ~/.bashrc to make this change permanent: $ vi .bash_profile function httpdcount { ps aux | grep httpd | grep -v grep | wc -l } export PS1='\u@\h <nowiki>[`httpdcount`]> </nowiki>' =='''Hack 43. Use shell script inside PS1 variable'''== 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''' '''<nowiki>[\$(totalfilesize.sh) </nowiki>bytes]> "''' ramesh@dev-db <nowiki>[534 </nowiki>bytes]> cd /etc/mail ramesh@dev-db <nowiki>[167997 </nowiki>bytes]> <nowiki>[</nowiki>'''Note''': This executes the totalfilesize.sh to display the total file size of the current directory in the PS1 prompt]
Summary:
Please note that all contributions to TetraWiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
TetraWiki:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)