BASH Prompt Customization

General Information

In this short tutorial I shall explain the basics of BASH prompt customization. I won’t cover the more advanced aspects, and probably won’t cover colours, but just enough to make your prompt display the information that you want to know.

Requirements

  1. Access to the server
  2. BASH installed
  3. pico installed

Meeting .profile

I installed FreeBSD 4.9 on my new server a few days ago, and installed BASH for users (I use csh for root – but that’s another topic). When I created a user, their prompt would look like this:

-bash-2.05b$

Rather boring isn’t it? And it doesn’t provide me any information. Right, let’s get down to business. The BASH shell prompt is controlled by your .profile file in your home directory (e.g. if my username was configure it would be /home/configure/).

First of all, let’s open up our .profile to get to know it a little better. Use your favourite text editor, but I’ll use pico with the -w switch to disable wrapping.

# pico -w .profile

Take a look around the file, it should look something like this:

# $FreeBSD: src/share/skel/dot.profile,v 1.19.2.2 2002/07/13 16:29:10 mp Exp $
#
# .profile - Bourne Shell startup script for login shells
#
# see also sh(1), environ(7).
#

# remove /usr/games and /usr/X11R6/bin if you want
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin:/usr/X11R6/bin:$HOME/bin; export PATH

# Setting TERM is normally done through /etc/ttys.  Do only override
# if you're sure that you'll never log in via telnet or xterm or a
# serial line.
# Use cons25l1 for iso-* fonts
# TERM=cons25;  export TERM

BLOCKSIZE=K;    export BLOCKSIZE
EDITOR=pico;    export EDITOR
PAGER=more;    export PAGER

# set ENV to a file invoked each time sh is started for interactive use.
ENV=$HOME/.shrc; export ENV

Customization

Okay then, let’s start customizing our prompt. BASH reads from the PS1 variable to determine what to display as your prompt; if there isn’t a PS1 variable, it displays -bash-2.05b$.

First I shall show you an example of a simple prompt, and then explain the parts.

PS1="\u@\h:\w$ "; export PS1
configure@bsd:/home/configure$

On the first line, I show what we put in .profile – PS1= defines the PS1 variable, and export PS1 exports the variable for usage in the shell. On the second line, I show what the prompt would look like with that specific PS1 variable. configure is my username, bsd is the short hostname and /home/configure is my current working directory.

You can however, include a whole range of data in your shell prompt. Now I shall explain the values and what each one displays.

a
Makes the computer "beep".
\d
Displays the date in "Weekday Month Date" format.
\h
Displays short hostname.
\H
Displays complete hostname including domain.
\w
Displays current working directory.
\n
Makes the prompt move to a new line.
\s
Displays the name of the shell, e.g. BASH
\t
Displays the current time in 24 hour HH:MM:SS format.
\T
Displays the current time in 12 hour HH:MM:SS format.
\@
Displays the current time in 12 hour am/pm format.
\u
Displays your username.
\!
Displays the number of commands in the history file (.bash_history).
\#
Displays the number of commands you have executed in your current session.

So there are your values; experiment! A more complex prompt would be:

PS1="[\t - \d]\n\u@\H:\w[\s]$"

[11:04:12 - Monday September 17]
configure@bsd.mydomain.com:/home/configure[BASH]$

Go on, have some fun, I may write another tutorial later on about including colours in your BASH prompt. I hope I have been of some help.

Speak Your Mind

*