Free Hosting Online for WorkStations

< Previous | Contents | Next >

Trying Some Alternative Prompt Designs

With this list of special characters, we can change the prompt to see the effect. First, we'll back up the existing prompt string so we can restore it later. To do this, we will copy the existing string into another shell variable that we create ourselves:



[me@linuxbox ~]$ ps1_old="$PS1"

[me@linuxbox ~]$ ps1_old="$PS1"


We create a new variable called ps1_old and assign the value of PS1 to it. We can ver- ify that the string has been copied by using the echo command:


[me@linuxbox ~]$ echo $ps1_old

[\u@\h \W]\$

[me@linuxbox ~]$ echo $ps1_old

[\u@\h \W]\$


We can restore the original prompt at any time during our terminal session by simply re- versing the process:



[me@linuxbox ~]$ PS1="$ps1_old"

[me@linuxbox ~]$ PS1="$ps1_old"


Now that we are ready to proceed, let's see what happens if we have an empty prompt string:



[me@linuxbox ~]$ PS1=

[me@linuxbox ~]$ PS1=


If we assign nothing to the prompt string, we get nothing. No prompt string at all! The prompt is still there, but displays nothing, just as we asked it to. Since this is kind of dis - concerting to look at, we'll replace it with a minimal prompt:



PS1="\$ "

PS1="\$ "


That's better. At least now we can see what we are doing. Notice the trailing space within the double quotes. This provides the space between the dollar sign and the cursor when

Trying Some Alternative Prompt Designs


the prompt is displayed.

Let's add a bell to our prompt:



$ PS1="\[\a\]\$ "

$ PS1="\[\a\]\$ "


Now we should hear a beep each time the prompt is displayed. This could get annoying, but it might be useful if we needed notification when an especially long-running com- mand has been executed. Note that we included the \[ and \] sequences. Since the ASCII bell (\a) does not “print,” that is, it does not move the cursor, we need to tell bash so it can correctly determine the length of the prompt.

Next, let's try to make an informative prompt with some hostname and time-of-day infor- mation:



$ PS1="\A \h \$ "

17:33 linuxbox $

$ PS1="\A \h \$ "

17:33 linuxbox $


Adding time-of-day to our prompt will be useful if we need to keep track of when we perform certain tasks. Finally, we'll make a new prompt that is similar to our original:



17:37 linuxbox $ PS1="<\u@\h \W>\$ "

<me@linuxbox ~>$

17:37 linuxbox $ PS1="<\u@\h \W>\$ "

<me@linuxbox ~>$


Try out the other sequences listed in the table above and see if you can come up with a brilliant new prompt.


Top OS Cloud Computing at OnWorks: