fluidmind
Star God: Fu Star God: Lu Star God: Shou

The bash-prompt Shell Program

Copyright © 2013 by Daniel G. Delaney, Fluid Mind Software

Current Version: 2.0.0
Homepage:http://fluidmind.org/software/bash-prompt/
Written by:Daniel G. Delaney
Last updated:July 2013
Status:Freeware
This program may be freely used and distributed, as long as this copyright information is not modified. If you have suggestions/bug reports, please contact the author and suggest them for the official distribution.

The bash-prompt program is a small C program meant to be used with Bash's PROMPT_COMMAND option. It displays the hostname and current working directory, justified to the left of the screen, and the time and date justified to the right of the screen. If the current working directory is too long to fit in that space, it truncates the beginning of it and replaces it with an ellipsis.

Here's a sample of what it outputs:

hostname:~/current/working/directory/                 07/25/2013 09:14:22 AM

I wrote the first version of this little program sometime around 1997 or '98 and forgot all about it as I migrated from one server to the next over the years. I recently re-discovered it tucked away in a long-forgotten corner of my personal server, and decided to have a little fun updating it. I've re-worked the string handling by replacing most of the static strings with malloc(), and making it handle Unicode (UTF-8) directory names correctly with the wchar.h library.

How to Get It

bash-prompt is a very simple program, only a single C source code file. You can either download the C source code as a text file or as a gzipped tarball (you might have to right-click those links). Or you can download it from the command line with the following:

curl http://fluidmind.org/software/bash-prompt/bash-prompt.c > bash-prompt.c

All you have to do is compile it with any standard ANSI C compiler (GNU C works fine) using the following command:

cc bash-prompt.c -o bash-prompt

This will produce an executable named "bash-prompt". Just move that into your /usr/local/bin directory.

I've also made a few pre-compiled binaries available for various platforms to which I have access. Just unzip/untar them right into your cgi-bin directory.

You can download those from the command line with one of the following commands:

wget http://fluidmind.org/software/bash-prompt/binaries/bash-prompt_2.0.0_linux_x64.tgz
wget http://fluidmind.org/software/bash-prompt/binaries/bash-prompt_2.0.0_macosx_intel64.tgz

How to Use It

Add the following lines to either ~/.profile or your /etc/profile, and remove or comment out the line that defines PS1.

# If bash-prompt program is present, use it
if [ -f /usr/bin/bash-prompt ] || [ -f /usr/local/bin/bash-prompt ]; then
    PROMPT_COMMAND='bash-prompt'
    PS1=''
else
    PROMPT_COMMAND=''
    PS1='\[\e]0;\h\a\]\n\w     \D{%x %X}\n'
fi

# If we're in a screen, the display its number
if [ -v "WINDOW" ]; then
    PS1="$PS1\u:${WINDOW}> "
else
    PS1="$PS1\u> "
fi

Safety Concerns

With all C programs, you should never run them on your machine without making sure they don't pose a threat due to memory leaks or buffer overflows. I've refactored the large fixed strings into dynamically allocated strings (with malloc()). The following output is from the valgrind memcheck error detector, showing that this program has no memory leaks.

Memcheck, a memory error detector
Using Valgrind-3.7.0 and LibVEX; rerun with -h for copyright info
Command: bash-prompt

HEAP SUMMARY:
    in use at exit: 0 bytes in 0 blocks
  total heap usage: 47 allocs, 47 frees, 10,922 bytes allocated

All heap blocks were freed -- no leaks are possible

ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 2 from 2)