How to Set Linux/Unix

Command Prompts

for Terminals

(adding colors and
current directory name)

Home > RefInfo menu > Computer topics menu >

Handy Commands and Scripts menu >

This 'How to Set Linux Command Line Interface (CLI) Prompts' page

More examples may be added and some examples changed,
if I re-visit or use this page.

INTRODUCTION :

Below is information on setting Linux/Unix CLI (Command Line Interface) prompts --- the prompts that one sees in a 'terminal' when the terminal is waiting for your next command.

The environment variable name for the 'primary' prompt is 'PS1'. The environment variable name for the 'secondary' prompt is 'PS2'.

This page is mainly concerned with setting 'PS1'.

The variable 'PS1' is typically set in an assignment statement (an equal sign statement) in a 'run-control' (rc) file in the user's home directory.

The name of the 'rc' file is typically

  • '.bashrc' or
  • '.profile' or
  • '.bash_aliases' (for example, in Ubuntu 9.10)

The 'rc' file is usually a 'dot' file --- a 'hidden' file --- in the user's home directory.

The default is usually to have the prompt occupy one line on the terminal per prompt --- with the prompt being a white dollar-sign on a black background --- or a black dollar-sign on a white backgroud.

The image at the top of this page indicates that the prompt can 'occupy' multiple lines, such as, in the example image above:

  • a blank line
  • a line showing the current working directory, in yellow
  • a line showing the usual prompt, a dollar-sign, in white

Here is an image that shows how the directory changes as one uses the 'cd' command to change to different directories.

In this image, the first prompt shows that we are positioned in the home directory --- indicated by the tilde character, '~'.

We issued the 'cd Documents' command and the next prompt shows that we are positioned at '~/Documents' --- the 'Documents' directory in the user's home directory.

Then we issued the 'cd ..' command to move back up a directory and the next prompt shows that we are positioned back in the home directory --- indicated by the tilde, '~'.

Then we issued the 'cd ..' command AGAIN to move up a directory level and the next prompt shows that we are positioned in the '/home' directory --- the parent directory of all the user home directories.

Here is how the 'PS1' prompt was set to do this. This is a combined 'export' and assignment (equals sign) statement. Note that it is 3 lines --- corresponding to the 3 lines we see for each prompt in the image above.


export PS1='\e[0;33;48m
\w\e[0;37;48m
\$ '

Explanation:
The first line --- the '\e[0;33;48m' --- causes any following text to be shown in yellow (33). The line-feed at the end of that line causes a blank line to be displayed.

The second line requests the current working directory (\w) to be shown and then sets the text color back to white (37) with '\e[0;37;48m'. The line-feed at the end of that line causes the directory name to appear on the line by itself.

The third line sets the command prompt to be a dollar-sign separated by a space. (The various back-slashes have special purposes and are needed.)

A little more detailed description appears in the comment statements below.


Some handy Shell-Prompt-setting code ... to add to
a '~/.bashrc' or '~/.bash_aliases' or '~/.profile' file :


##
## The following PS1 var changes the text color to yellow (33) and adds a line-feed
## before showing the current working directory (\w). Then it sets the text color
## back to white. A black background is specified in both color changes.
##      33=yellow-foreground 37=white-foreground 48=black-background
##
## '\e' represents an escape code, so that following character codes are not
## simply displayed as-is.
## The line-feeds are indicated by the change to the next line.
##
## This code provides 3 lines for each command prompt:
## 1 - a blank line (to separate from previous command output or command entry)
## 2 - the current working directory name, in yellow on a black background
## 3 - the dollar-sign command prompt (and a space), on a line by itself.
##
## You could switch 48 and 37 --- black and white.
##

export PS1='\e[0;33;48m
\w\e[0;37;48m
\$ '

## The above works in a bash or Korn shell.
## For a Bourne shell, you may have to break this up into two statements
## an 'export' statement after the assignment statement:
##

PS1='\e[0;33;48m
\w\e[0;37;48m
\$ '

export PS1


Here is a link to help search for more information on setting the PS1 and PS2 environment variables for the command-line prompts.

Bottom of this
How to Set Linux/Unix Command Prompts for Terminals page.

To return to a previously visited web page location, click on the Back button of your web browser a sufficient number of times. OR, use the History-list option of your web browser.
OR ...

< Go to Top of Page, above. >

Page was created 2009 Oct 28. (As part of a commands-and-scripts menu page.)
Page was changed 2017 Sep 18. (Split this info into a separate page.)
Page was changed 2018 Sep 01. (Added css and javascript to try to handle text-size for smartphones, esp. in portrait orientation.)