Task:display current BASH prompt (PS1)
Use the echo command to display current BASH prompt:
$ echo $PS 1
Sample outputs:
[\\u@\h \\w]\\$
This is another output from my Debian based system:
$ echo $PS 1
Sample outputs:
\[\e]0;\u@\h: \w\a\]${debian_chroot:+ ($debian _chroot)}\u@\h:\w\$
By default the command prompt be set to [\u@\h \w]\$. The backslash-escaped special characters are decoded as follows: \u: Display the current username. \h: Display the hostname \w: Print The base of current working directory. \$: Display # (indicates root user) if the effective UID is 0, otherwise display a $. task:modify current BASH prompt
Use the Export command to setup a new shell prompt:
$ Export ps1= "[\\u@\\h \\w \\@]\\$"
Sample outputs:
Fig.01:new Prompt in action
Where, \h: Display FQDN hostname. \@: Display the 12-hour am/pm format task:add colors to the prompt
To add colors to the shell prompt use the following export command syntax:
' \e[x;ym $PS 1 \e[m '
Where, \e[: Start color scheme. X;y:color pair to use (x;y) $PS 1:your shell prompt variable. \e[m:stop color scheme .
To set a red color prompt, type the following command:
$ export ps1= "\e[0;31m[\u@\h \w]\$ \e[m"
Sample outputs:
Fig.02:adding the colors to the prompt A list of color codes
Color |
Code |
Black |
0;30 |
Blue |
0;34 |
Green |
0;32 |
Cyan |
0;36 |
Red |
0;31 |
Purple |
0;35 |
Brown |
0;33 |
Blue |
0;34 |
Green |
0;32 |
Cyan |
0;36 |
Red |
0;31 |
Purple |
0;35 |
Brown |
0;33 |
Note:you need to replace digit 0 and 1 to get light color version. task:how do I make the prompt setting permanent?
The
Your new shell prompt setting set by $PS 1 are temporary i.e. when you logout setting'll be lost. To have it set every time for login to your workstation add above Export command to your $HOME/.bash_profile file or $HOME /.BASHRC file.
$ cd
$ VI bash_profile
OR
$ vi $HOME/.BASHRC
Append The following line:
Export ps1= \e[0;31m[\u @\h \w]\$ \e[m "
Save and close the file. Say Hello to tput command
You can also use Tput command to set terminal and modify the prompt settings. For example, to display RED color prompt using a tput:
Export ps1= \[$ (tput setaf 1) \]\u@\h:\w $ \[$ (tput sgr0) \] " 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, check color {CODE} table below for more information. Tput Setab {code}-Set background color, check color {CODE} table below for more information. various color codes for the Tput command
Color {Code} |
Color |
0 |
Black |
1 |
Red |
2 |
Green |
3 |
Yellow |
4 |
Blue |
5 |
Magenta |
6 |
Cyan |
7 |
White |
recommend readings
Bash allows prompt strings to is customized by inserting a number of backslash-escaped special characters that are D Ecoded as follows:\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\e: An ASCII escape character (033)\h: the hostname up to the "."\h: the hostname\j: The number of jobs currently managed by the Shell\l: The basename of the shell ' s terminal device name\ n: NewLine\ r: Carriage return\s: The name of the shell, the basename of $ (the portion following the final slash)\ 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\a: The current time in 24-hour hh:mm format\u: The username of the current user\v: The version of Bash (e.g., 2.00)\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\!: 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 is used to embed a terminal control sequence into the prompt\]: End a sequence of non-printing characters