[GRLUG] Custom $PS1 settings

Raymond McLaughlin driveray at ameritech.net
Wed May 16 02:17:06 EDT 2007


Michael Mol wrote:
> Like, I suspect, many people, I have custom settings for PS1 in my
> ~/.bashrc file:
> 
> PS1="\u@\H\w\n\t \$ "
> 
> This give me a conventient prompt:
> 
> shortcircuit at caterpillar~
> 22:53:34 $
> 
> I see my username, the name of the machine I'm logged into, my path,
> the current time, and whether or not I'm logged in as root.
> 
> In the past, I've had prompts which executed scripts that displayed an
> indicator to tell me whether or not certain accounts were logged in.
> 
> What do you use for a prompt?  What's the most unique prompt you've ever used?

Glad you asked, this is one of my favorite hacks. Most people know about
the $PS1 variable, but few know about the $PROMPT_COMMAND variable.
Where $PS1 defines how the prompt line looks, $PROMPT_COMMAND can define
a script to be run immediately before the command prompt is displayed.

In my case $PROMPT_COMMAND=prompt_command where 'prompt_command' is a
function defined in a startup script named .prompt_command which is in
turned called from a line in .bashrc (test -s ~/.prompt_command && .
~/.prompt_command). In addition to setting $PS1 to show which device has
the current console, it defines a function prompt_command to write
$USERNAME@$HOST:$PWD , in colored text on contrasting background, at the
top right corner of the display.

below is my startup script .prompt_command. I have been tweeking it for
a couple years. Note that I pasted it as a quotation to guard against
line wrap mangling. Each line begins with a leading '>' that should not
be included in the actual script.

> # bash recognizess a variable called PROMPT_COMMAND and, if it is set,
> # executes whatever it evaluates to. This script needs to be called from .bashrc.
> # The first part defines a function, 'prompt_command', which evaluates
> # the current working and directory, (and hostname and username if desired), 
> # then writes it to the upper right corner of the display in contrasting color.
> # The variable PROMPT_COMMAND is then set equal to 'prompt_command'.
> # The second part provides a short prompt string indicating the current input device.
> 
> ##############################
> # Step 1: Define function.
> ##############################
> function prompt_command 
> {
> tput sc   # Save the current cursor position
> 
> # set background, forground color, bold
> if [ $UID = 0 ]
>   then tput setab 1 ; tput setaf 3 ; tput bold  # yellow text on red background for root
>   else tput setab 4 ; tput setaf 6 ; tput bold  # cyan text on blue background for users
> fi
> 
> # Define DIR as current directory, with current users home reduced to ~
> DIR=$(pwd | sed s?$(echo ~)?~?)
> 
> # OLDDIR is directory string most recently WRITTEN TO THE SCREEN.
> # If $OLDDIR longer than the current $DIR it will be necessary to clean up artifacts,
> # which might remain to the left of the new one, by over-writing with blank spaces.
> if [ -z $OLDDIR  ]; then OLDDIR=$DIR; fi # $OLDDIR must not be empty.
> DIFF=$(($(echo $OLDDIR | wc -m)-$(echo $DIR | wc -m)))
> 
> ## Define SPLASH_STRING - pick one
> # SPLASH_STRING="$DIR"
> # SPLASH_STRING="$HOSTNAME:$DIR"
> user=$(echo ~ | sed s?/home?? |sed s?/??)
> 
> SPLASH_STRING="$user@$HOSTNAME:$DIR"
> 
> # Define pad as the total width of the terminal less the length
> # of the splash string
>  
> if [ $DIFF -gt 0 ];
>   then
>     let pad=$(tput cols)-$(echo $SPLASH_STRING | wc -m)-$DIFF+1
>     tput cup 0 $pad  # position the cursor so that the hostname and PWD are at top left (Y=0, X=$pad)
>     while [ $DIFF -gt 0 ]; do echo -n " "; DIFF=$(($DIFF-1));done
> 
>   else
>     let pad=$(tput cols)-$(echo $SPLASH_STRING | wc -m)+1
>     tput cup 0 $pad  # position the cursor so that the hostname and PWD are at top left (Y=0, X=$pad)
> fi
> 
> echo -n "$SPLASH_STRING"   #Show the hostname and present working directory.
> 
> OLDDIR=$DIR   # Save directory string for DIFF in next execution.
> tput rc   # Return the cursor to the saved position
> }
> 
> ########################################
> # Step 2: Set variable equal to function
> ########################################
> PROMPT_COMMAND=prompt_command
> 
> ############################################################
> # Step 3: Set prompt string to display current input device
> ############################################################
> 
> #Evaluate current logged device from output of 'tty'
> tty=$(echo $(tty) |sed s%/dev/%%)
> 
> # Set command prompt to show current console
> PS1="$tty \\$ "





More information about the grlug mailing list