BASH and the PS1 Variable

What is a shell, and why do I care about its prompt? A shell is nothing more than a program, serving two purposes. One is to interact with human beings. The other is to prep and start other programs. For the former, a shell program spends most of its life waiting for input from the keyboard. Every time a command is entered by a person, it runs the command and returns. Every time this interaction occurs, a prompt is returned for the person to see. This prompt can be anything you desire it to be.

Over the years, I have changed my shell prompt. My preference of shell programs is the BASH shell.

BASH provides an environment variable, PROMPT_COMMAND, that, according to the man page, “if set, the value is executed as a command prior to issuing each primary prompt.”

So here is my PROMPT_COMMAND, authored for Linux. I define a function, set_prompt(), and point the PROMPT_COMMAND variable to it:

The result is this:

The first line’s entries consist of:

    Exit status of previous command – turns bold red if anything other than 0
    Number of jobs running in background
    Load 1M
    Network with subnet mask
    Year Month Day
    Day Of Week
    Time
    DiskvName
    % Disk Free
    Free Disk in Human Readable Format

There are two fields here of note. The first is the exit status of the previous command. This is useful for tar, ssh, and rsync commands. The date is also very useful. Let’s say a command completes when you are away from your desk. Now, you’ll know when. The disk usage is also nice, but comes with a caveat. If any filesystem becomes unresponsive, so does your shell.

The second line contains:

    User @ Hostname
    TTY
    Present working directory
    IP address remote Host, if remotely connected
    Remote port from remote host, if remotely connected
    Local port from remote host, if remotely connected

Comments are closed.