In the Bash shell, there as a programmable prompt (so to speak). It uses the variable named PS1 and PS2. For a beginner, its good to know how to modify the prompt displayed, change its colours and applying it to a single user or as default for all. Bash 4.2 added support for unicode which means way more fonts are available to make pretty prompts.
Basics
A simple assignment of the prompt is as follows;
export PS1='Demo$ '
The space after the '$' is commonly used to make typing more readable.
The prompt can be assigned with the Bash shell special characters. Some are visible and other not. Following list some of these characters;
\d the date in "Weekday Month Date" format (e.g., "Tue May 26")
\e an ASCII escape character (033)
\h the hostname up to the first '.'
\H the hostname
\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
\w the current working directory
\W the basename of current working directory
Example 1: Common prompt lets user know their login user name, server host name and working directory.
export PS1='[\u@\h \W]\$'
Example 2: Display time the prompt was executed.
export PS1='\t:\w$ '
Example 3: A two line prompt which documents the last history number.
export PS1='=\d \u@\h \!=\n\t $ '
Colours
The prompt supports colours as defined by ANSI (see linuxhowtos.org). This is in the format to display colours;
\[\ecolourcode\].
Example of ANSI colours
Black 0;30m Dark Gray 1;30m
Red 0;31m Bold Red 1;31m
Green 0;32m Bold Green 1;32m
Yellow 0;33m Bold Yellow 1;33m
Blue 0;34m Bold Blue 1;34m
Purple 0;35m Bold Purple 1;35m
Cyan 0;36m Bold Cyan 1;36m
Light Gray 0;37m White 1;37m
The first digit 0=normal, 1=bold, 4=underline and each code ends with the letter 'm'. At end of prompt, return to the default colour, with \[\e[m\]
Colour 40m onwards can be used to highlight the background.
Example 1: Prompt is in blue and user types in blue.
export PS1='[\[\e[0;34m\]\u@\h \W]\$ '
Example 2:Prompt is in blue and user types using default font colours.
export PS1='[\[\e[0;34m\]\u@\h \W]\$\[\e[m\] '
Example 3: Highlight text in prompt
export PS1='\[\e[1;34m\]\u\[\e[1;33m\]@\[\e[1;32m\]\h\[\e[1;37m\]:\[\e[1;31m\]\w \[\e[1;36m\]\$ \[\e[m\]'
Example 4: Blue background with red text.
export PS1='\[\e[44m\]\[\e[1;31m\][\u@\h \W]\$\[\e[m\] '
Saving changes
This PS1 value can be set in /etc/bashrc to be used by all users. Each user can change their prompt by editing the file ~/.bashrc with this PS1 or PS2 assignment.Example: ~/.bashrc file
# .bashrc
# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
# User specific aliases and functions
export PS1='[\[\e[0;34m\]\u@\h \W]\$\[\e[m\] '
Advanced prompt
To run a command before the bash prompt is displayed, use PROMPT_COMMAND. This supports many more flexible formatting of values for advance Linux administrators.
Example:
PROMPT_COMMAND='printf "\033]0;%s@%s:%s\007" "${USER}" "${HOSTNAME%%.*}" "${PWD/#$HOME/~}" ' \
export PS1='[\[\e[0;34m\]\u@\h \W]\$\[\e[m\] '
Example 2: Charles Torvalds Basic Power Prompt
PROMPT_COMMAND='history -a;echo -en "\033[m\033[38;5;2m"$(( `sed -n "s/MemFree:[\t ]\+\([0-9]\+\) kB/\1/p" /proc/meminfo`/1024))"\033[38;5;22m/"$((`sed -n "s/MemTotal:[\t ]\+\([0-9]\+\) kB/\1/Ip" /proc/meminfo`/1024 ))MB"\t\033[m\033[38;5;55m$(< /proc/loadavg)\033[m"' \
PS1='\[\e[m\n\e[1;30m\][$$:$PPID \j:\!\[\e[1;30m\]]\[\e[0;36m\] \T \d \[\e[1;30m\][\[\e[1;34m\]\u@\H\[\e[1;30m\]:\[\e[0;37m\]${SSH_TTY} \[\e[0;32m\]+${SHLVL}\[\e[1;30m\]] \[\e[1;37m\]\w\[\e[0;37m\] \n($SHLVL:\!)\$ '
Unicode
If you have Bash 4.2 onwards, then unicode characters can be added. First find a UTF 8 bit unicode you like from UTF8 or unicodelookup. Bash provide good number of symbols but does not map to 100% of the unicodes.Ramblings...In UTF-8 unicode for U+2620, Hex is 2620 in Octal is 023040, in Decimal is 9760 and HTML is ☠.
Example 1: Display skull and bones U+2620 or UTF-8 for URI encoding is E2 98 A0. The Octal equivalent is 342 230 240.
export PS1='[\u@\h \W]\342\230\240 '
Example 2: Display helm symbol U+2388 (See unicode) which is UTF-8 for URI encoding e2 8e 88
export PS1='[\u@\h \W]\342\216\210 '
Example 3: Multi line prompt
export PS1='\342\214\210\d \t \W\342\214\211\n\342\216\210 '
Done.
No comments:
Post a Comment