FRJ's Linux BASH

Return to Linux Page

Descended from the Bourne Shell, Bash is a GNU product, the "Bourne Again SHell."  It's the standard command line interface on most Linux machines. 

Bash Reference Manual An online BASH manual can be found here.

Remember the DOS C:> prompt? Not very helpful, right?  But if you were clever you could change it to tell you what directory you were in.  We are going to see how to change the bash command line interface's prompt.

Before we get started, it's important to remember that the PS1 string is stored in the environment like any other environment variable. 

The Linux enviroment, unlike the DOS enviroment, is very large. Type a set command and you will see what I mean. Lots of space !! You might want to do a set | more to see it all.

If you modify PS1 at the command line, your prompt will change. Before you make any changes, you can save your current prompt to another environment variable:

     [frj2@localhost frj2]$ tmp=$PS1          #read as tmp = the contents of PS1
     [frj2@localhost frj2]$

The simplest prompt would be a single character, such as:

     [frj2@localhost frj2]$ PS1="# "
     # ls
     bin   mail
     #

There are a number of special characters you can set the bash prompt to. The following is a partial list.

            \a     an ASCII bell character (07)
            \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
            \n     newline
            \r     carriage return
            \s     the name of the shell, the  basename  of  $0
                   (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
            \u     the username of the current user
            \v     the version of bash (e.g., 2.00)
            \V     the  release  of  bash, version + patchlevel
                   (e.g., 2.00.0)
            \w     the current working directory
            \W     the basename of the current  working  direc-
                   tory
            \!     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 be used to embed a terminal con-
                   trol sequence into the prompt
            \]     end a sequence of non-printing characters
So we might want to set PS1 to show the time of day and the current working directory.
     [frj2@localhost frj2]$ PS1="[\t \W] "
     [12:24:33 frj2 ] ls
     bin   mail
     [12:24:34 frj2 ]   
To set back to what it was before we began this madness, do this:
     [12:27:33 frj2 ] PS1=$tmp          #read as set PS1 = the contents of tmp
     [frj2@localhost frj2]$
Actually bash sets the prompt (PS1) to "[\u@h \W]\$" so if this blows up reset it to that. Of course you could just logout and log back in if you are lazy.