It is currently Tue, 17 May 2022 18:58:20 GMT

Author Message
 Changing your primary prompt
Is there anyway that I can create a Bourne Shell script that will change my
primary prompt when a user changes directories.  In theory the following
script should work:

cd $1
PS1="`pwd` "

but it doesn't, any clues.


 Tue, 14 Oct 2003 08:00:46 GMT   
 Changing your primary prompt


 Tue, 14 Oct 2003 08:17:13 GMT   
 Changing your primary prompt

Nope. Bourne shell does not re-evaluate PS1 variable for variable
substitution on a display by display basis.

Here is my Bourne shell function for showing the directory _and_ the time
in the prompt. Signal 30 in the script is to mean the configuration for
SunOS for SIGUSR1. Change this number for your particular system.


#! /bin/echo error: only source
#*TAG:50041 2:Oct 17 1998:0644:mkprompt_sh:
# Author: Brian Hiles <>
# Copyright: (c) 1998
# Date: 1998/10/02
# Description: put the time in PS1 -- bourne shell version
# Name: mkprompt_sh
# Requires: autoload, mapsig, require
# Sccs: @(#) 1.1 1998/09/21 (Brian Hiles)
# Usage: mkprompt_sh
# Version: 1.01

mkprompt_sh() # [-]
{       # uses global variables _prevsec and _ps1
        # define custom prompt here: $1=hours $2=minutes $3=seconds
        _force= _sleep= : ${_ps1:='[$1:$2]$ '}
        #_force= _sleep= : ${_ps1:=' [7m$1:$2$ [m '}
        test X$1 = X- && _force=ON shift
        set -- `date '+%H %M %S'`
        if test X$_force = XON -o 0${_prevsec:-60} -gt 0$3
        then    eval PS1="$PWD \"$_ps1\"" _sleep=`expr 50 - $3`
                test 0$_sleep -le 0 && _sleep=5
        else    _sleep=49       # default period
                case $3 in
                0?)     eval PS1="$PWD \"$_ps1\"" ;;
                1?)     _sleep=39 ;;
                2?)     _sleep=29 ;;
                3?)     _sleep=19 ;;
                4?)     _sleep=7 ;;
                5[0-5]) _sleep=3 ;;
                5?)     _sleep=2 ;;
        eval ${DIAG:+'echo [ mkprompt_sh: _prevsec=$3 sleeptime=$_sleep ] >&2'}
        (sleep $_sleep; kill -30 $$) </dev/null &

cd() # [dir]
{       chdir "$@" && PWD=`pwd`

# Warning: invoking mkprompt_sh produces the following side-effects:
cd                                      # set PWD to cwd
trap mkprompt_sh 30                     # USR1 signal subsumed for prompt
mkprompt_sh -                           # start process of time in prompt

: '
        mkprompt_sh - a mkprompt(3S) for sh(1)









        An "alias" for the builtin "cd" is defined that uses the undocumented
        builtin "chdir" that is implemented only in sh(1), not ksh(1).

        It is hardcoded within sh(1) that if the shell detects it is in an
        interactive environment, the PID of an invoked background process
        will be printed to /dev/tty, and thus cannot be redirected.

 Tue, 14 Oct 2003 11:50:11 GMT   
 Changing your primary prompt

Might I suggest "man bash," then go to the section titled PROMPTING? There's a
special prompting facility in bash that takes care of those things:

          \w   the current working directory

Somewhere in your .bashrc:

PS1="\w" export PS1

 Wed, 15 Oct 2003 00:12:50 GMT   
 Changing your primary prompt
This creates a nice PS1 prompt.  It works is a ksh... not sure about a sh.  Give it a try:

        bold=`tput smso`
        nobold=`tput rmso`
        export bold nobold
        UNAME=[`uname -n`]
        export PS1=$LOGNAME@$bold$UNAME$nobold'$PWD> '

 Thu, 16 Oct 2003 12:25:02 GMT   
 Changing your primary prompt
What do you mean by "nope"? Do you mean that if I use PS1='$PWD>', it will
not work in bash (Bourne Again sh)?

 Thu, 16 Oct 2003 23:40:15 GMT   
 Changing your primary prompt

He said Bourne shell, not bash.  Bash is a newer shell with many more
features.  (However, on some systems /bin/sh is a link to bash.  When
invoked as sh, it pretends to be an old Bourne shell.)

 Fri, 17 Oct 2003 03:48:19 GMT   
 Changing your primary prompt
Oh, I see.

Then Bourne shell needs to be replaced with bash whenever possible.

 Fri, 17 Oct 2003 04:26:35 GMT   
 Changing your primary prompt
How about (for ksh):
    export REV=`tput rev`  REG=`tput sgr0`
    PS1="$REV!:(`uname -n`):\${PWD##*/}$REG $ "

 Fri, 17 Oct 2003 21:33:34 GMT   
 Changing your primary prompt

Hold on there!

While an admirable addition to the community at large, free bash
shell elides a great deal many bourne shell syntactic subtleties,
breaking all of those scripts assuming bourne shell parsing and
environment handling.

For instance, the notion of "special shell builtins" is not present
in bash not invoked with a special --posix option.

In the great scheme of things, not a big deal -- except for scripts
that can do major damage executing under the entirely reasonable
expectation of following (eternally) documented verities.


 Sat, 18 Oct 2003 08:37:54 GMT   
   [ 10 post ] 

Similar Threads

1. Changing login prompt (NOT motd or shell prompt)

2. How can I change text login in prompt to GUI login prompt

3. Change primary disk - fix LILO?

4. How to change primary nic?

5. How-To change Primary NIC Interface?

6. How to change the primary interface

7. changing DNS primary

8. Changing root's Primary Group?

9. changing primary group to secondary

Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software