In two previous articles, Gentoo OpenRC Installation and Bash 02 – Variables and Such, I mentioned the use of the Prompt String 1, 'PS1', to change the prompt for the Command-Line Interface (CLI).
Let's look at this in more detail to get your prompt to be more specific to what you want it to be in the terminal.
Configuration File
Before we look into what we want the prompt to be, we need to see the current setting.
From a terminal, you can use the command:
Some Shells, such as Fish, do not display the contents of PS1 when you use the command. The next place is to look in the configuration file. These are not stored in the same place, so I will list a few default configuration files:
You can use your favorite editor and set the prompt for the variable 'PS1'.
Parameters
There are a few simple parameters you can use to get the output you would prefer for your CLI.
For Ubuntu, the default shell is BASH. BASH has the default prompt of 'Username@Hostname
WD$'. In my case, my prompt is 'Jarret@Ubuntu:~$'. The Username and Hostname are in green and the Present Working Directory (PWD) is in blue.
So, let's look at the parameters first. There are about 24 parameters that can be used:
An example output would be '6-25 14:47'.
Color
We can incorporate color into our prompt. There are three main codes: 3x,4x and 1xx. The 3x range is for foreground color, or the color of the font. When using the 4x color scheme, this is the standard for background color. The 1xx range is the bright background colors. For the foreground:
FIGURE 1
We can combine foreground and background colors by using one of two methods:
FIGURE 2
The code to do this is as follows. Just notice that I changed the font color, so it did not blend in with the black background.
At the end of each line, there is a '\033[0m'. This is used to reset the colors back to the terminal default. This means I did not need to change the foreground back to black, since I changed to a light theme, which made the default foreground black and the background white. Figure 3 shows the light colors of '10#'.
FIGURE 3
Special Font Attributes
There are nine special attributes that can be used, depending on the terminal, some may not be supported:
FIGURE 4
First, it resets all attributes and colors, then prints 'Normal'. Before each text output, it sets the attribute needed and resets the attributes after the text is printed.
Create Prompt
Find the options you wish on the list. Determine what colors to use if you want color. Remember to use the reset at the end of the prompt, or else the color settings will be used for the text typed by the user. Export the options into the 'PS1' variable and test it. Once you have the options you want to use, set them in the configuration file as mentioned at the beginning of the article. Something like:
Conclusion
You can now set your own customized prompt for the terminal. If you change your shell, then the prompt default may change, so use one that suits you.
Once you use a terminal and specific shell, you will become accustomed to your prompt and if you change shells, then you will miss it. Experiment with the prompt, because there may be more information that you want to add or even remove from your default prompt.
Let's look at this in more detail to get your prompt to be more specific to what you want it to be in the terminal.
Configuration File
Before we look into what we want the prompt to be, we need to see the current setting.
From a terminal, you can use the command:
Code:
echo $PS1
Some Shells, such as Fish, do not display the contents of PS1 when you use the command. The next place is to look in the configuration file. These are not stored in the same place, so I will list a few default configuration files:
- BASH - ~/.bashrc
- ZSH - ~/.zshrc
- KSH - ~/.kshrc
- TCSH - ~/.cshrc
- DASH - ~/.dashcore/dash.conf
- BS - ~/.profile
- Fish Shell - ~/.config/fish/config.fish
- C Shell - ~/.cshrc
You can use your favorite editor and set the prompt for the variable 'PS1'.
Parameters
There are a few simple parameters you can use to get the output you would prefer for your CLI.
For Ubuntu, the default shell is BASH. BASH has the default prompt of 'Username@Hostname
So, let's look at the parameters first. There are about 24 parameters that can be used:
- \a - bell
- \d - date (format is Weekday Month Date)
- \D{format} - this is a time format that is set by the locale
- \e - escape character
- \h - hostname (only up to the first period in the name)
- \H - hostname
- \j - number of jobs in the shell
- \l - number of the terminal window opened, starts with '0'
- \n - newline
- \r - carriage return
- \s - name of the shell ($0)
- \t - time in the format of HH:MM:SS (24-hour)
- \T - time in the format of HH:MM:SS (12-hour)
- \@ - time in a 12-hour format (HH:MM) with AM/PM
- \A - time in 24-hour format (HH:MM)
- \u - current user's username
- \v - BASH version
- \V - BASH version and patchlevel
- \w - current folder (PWD) and all subfolders
- \W - current folder only, no subfolders listed
- \! - history number of the command (in the .histfile)
- \# - command number, or the number of commands executed since opening the terminal
- \$ - if a regular user, then it is a $, if it has elevated privileges then it is a #
- \nnn - displays the ASCII character for 'nnn', be sure it is three digits
- %Y - four digit year
- %y - two digit year
- %m - two digit month, padded with a zero
- %-m - month with no padding
- %d - two digit day, padded with a zero if needed
- %-d - day with no padding
- %D - full format of mm/dd/yy
- %h - first three letters of month name
- %b - abbreviation of current month
- %B - full current month name
- %H - Hour (24-hour), padded with a zero
- %-H - Hour (24-hour), no padding with a zero
- %I - Hour (12-hour), padded with a zero if needed
- %-I - Hour (12-hour), no padding with a zero
- %M - Minute, with zero padding
- %-M - Minute, with no zero padding
- %S - Seconds, with padding of a zero
- %-S - Seconds, with no zero padding
- %s - Seconds and milliseconds
- %f - milliseconds
- %p - AM or PM for use with 12-hour clock
- %a - day of week abbreviated name
- %A - day of week full name
- %w - weekday number (Sunday is 0 and Monday is 1, etc)
- %j - number of the day of the year (001-365), padded with zeros
- %-j - number of the day of the year (1-365), not padded with zeros
- %U - week number of the year (00-52), padded with zeros
- %-U - week number of the year (0-52), not padded with zeros
- %c - date and time according to the locale specified format
- %x - date according to locale specified format
- %X - time according to locale specified format
Code:
export PS1="\D{%-m-%y %-H:%M}"
Color
We can incorporate color into our prompt. There are three main codes: 3x,4x and 1xx. The 3x range is for foreground color, or the color of the font. When using the 4x color scheme, this is the standard for background color. The 1xx range is the bright background colors. For the foreground:
- 30 - Black
- 31 - Red
- 32 - Green
- 33 - Yellow
- 34 - Blue
- 35 - Magenta
- 36 - Cyan
- 37 - Light Gray (white)
- 40 - Black
- 41 - Red
- 42 - Green
- 43 - Yellow
- 44 - Blue
- 45 - Magenta
- 46 - Cyan
- 47 - Light Gray (white)
- 100 - Dark Gray
- 101 - Light Red
- 102 - Light Green
- 103 - Light Yellow
- 104 - Light Blue
- 105 - Light Magenta
- 106 - Light Cyan
- 107 - White
FIGURE 1
We can combine foreground and background colors by using one of two methods:
- \033[4#;3#m
- \e[4#;3#m
FIGURE 2
The code to do this is as follows. Just notice that I changed the font color, so it did not blend in with the black background.
Code:
export PS1="\033[40;37mBlack(40)\033[0m\n\033[41mRed(41)\033[0m\n\033[42mGreen(42)\033[0m\n\033[43mYellow(43)\033[0m\n\033[044mBlue(44)\033[0m\n\033[45mMagenta(45)\033[0m\n\033[46mCyan(46)\033[0m\n\033[47;30mWhite(47)\033[0m"
At the end of each line, there is a '\033[0m'. This is used to reset the colors back to the terminal default. This means I did not need to change the foreground back to black, since I changed to a light theme, which made the default foreground black and the background white. Figure 3 shows the light colors of '10#'.
FIGURE 3
Special Font Attributes
There are nine special attributes that can be used, depending on the terminal, some may not be supported:
- Bold (1m)
- Dim (2m)
- Italics (3m)
- Underlined (4m)
- Blink (5m)
- Slow Blink (6m)
- Invert (7m)
- Hidden (8m)
- Strikethrough (9m)
FIGURE 4
Code:
export PS1="\033[0mNormal\n\033[1mBold\033[0m\n\033[2mDim\033[0m\n\033[3mItalics\033[0m\n\033[4mUnderlined\033[0m\n\033[5mBlink\033[0m\n\033[6mSlow Blink\033[0m\n\033[7mInverse\033[0m\n\033[8mHidden\033[0m\n\033[9mStrikethrough\033[0m\n"
First, it resets all attributes and colors, then prints 'Normal'. Before each text output, it sets the attribute needed and resets the attributes after the text is printed.
Create Prompt
Find the options you wish on the list. Determine what colors to use if you want color. Remember to use the reset at the end of the prompt, or else the color settings will be used for the text typed by the user. Export the options into the 'PS1' variable and test it. Once you have the options you want to use, set them in the configuration file as mentioned at the beginning of the article. Something like:
Code:
PS1="\u@\h:\w\$"
Conclusion
You can now set your own customized prompt for the terminal. If you change your shell, then the prompt default may change, so use one that suits you.
Once you use a terminal and specific shell, you will become accustomed to your prompt and if you change shells, then you will miss it. Experiment with the prompt, because there may be more information that you want to add or even remove from your default prompt.

