1. Technology
You can opt-out at any time. Please refer to our privacy policy for contact information.

Discuss in my forum

Introduction to Linux

By Machtelt Garrels

7.2.4. The Bash prompt Introduction

The Bash prompt can do much more than displaying such simple information as your user name, the name of your machine and some indication about the present working directory. We can add other information such as the current date and time, number of connected users etc.

Before we begin, however, we will save our current prompt in another environment variable:


[jerry@nowhere jerry]$ MYPROMPT=$PS1
 [jerry@nowhere jerry]$ echo $MYPROMPT
 [\u@\h \W]\$
 [jerry@nowhere jerry]$

When we change the prompt now, for example by issuing the command PS1 ="->" , we can always get our original prompt back with the command PS1 =$MYPROMPT . You will, of course, also get it back when you reconnect, as long as you just fiddle with the prompt on the command line and avoid putting it in a shell configuration file. Some examples

In order to understand these prompts and the escape sequences used, we refer to the Bash Info or man pages.

  • export PS1 ="[\t \j] "

    Displays time of day and number of running jobs

  • export PS1 ="[\d][\u@\h \w] : "

    Displays date, user name, host name and current working directory. Note that \W displays only base names of the present working directory.

  • export PS1 ="{\!} "

    Displays history number for each command.

  • export PS1 ="\[\033[1;35m\]\u@\h\[\033[0m\] "

    Displays user@host in pink.

  • export PS1 ="\[\033[1;35m\]\u\[\033[0m\] \[\033[1;34m\]\w\[\033[0m\] "

    Sets the user name in pink and the present working directory in blue.

  • export PS1 ="\[\033[1;44m\]$USER is in \w\[\033[0m\] "

    Prompt for people who have difficulties seeing the difference between the prompt and what they type.

  • export PS1 ="\[\033[4;34m\]\u@\h \w \[\033[0m\]"

    Underlined prompt.

  • export PS1 ="\[\033[7;34m\]\u@\h \w \[\033[0m\] "

    White characters on a blue background.

  • export PS1 ="\[\033[3;35m\]\u@\h \w \[\033[0m\]\a"

    Pink prompt in a lighter font that alerts you when your commands have finished.

  • export PS1 =...

Variables are exported so the subsequently executed commands will also know about the environment. The prompt configuration line that you want is best put in your shell configuration file, ~/.bashrc .

If you want, prompts can execute shell scripts and behave different under different conditions. You can even have the prompt play a tune every time you issue a command, although this gets boring pretty soon. More information can be found in the Bash-Prompt HOWTO .

* License

* Introduction to Linux Index

  1. About.com
  2. Technology
  3. Linux

©2014 About.com. All rights reserved.