[comp.unix.questions] Prompt changing on the fly - Oops

lvc@danews.UUCP (01/18/87)

>What shell are you using?  It is possible in the Korn shell.
>Impossible in the Bourne shell.  I don't know about the C-shell.
>My prompt includes a clock that is updated at every return.

Later that same day:

Oops, it is possible in the Bourne shell.  What you can do
is define a function (not well known in the Bourne shell)
to do your cd's and set PS1.

chdir()
{
    case ${#} in
	1) cd $1 && PS1="`pwd` "
	   ;;
	*) echo "usage: chdir directory" >&2
	   return 1
	  ;;
    esac
}

Now just use chdir instead of cd. 

In the Korn Shell, you can do something like:

PS1='${PWD} '

This works because PS1 is *evaluated* every time it is printed.
You can use cd and PS1 is updated automatically.
-- 

Larry Cipriani   Cornet 353-4999 AT&T (614) 860-4999
{ihnp4|cbosgd}!cbsck!lvc   AT&T Network Systems rm 2B-220

chris@mimsy.UUCP (01/19/87)

In article <376@danews.ATT.COM> lvc@danews.ATT.COM (Larry Cipriani) writes:
>>Impossible in the Bourne shell.  I don't know about the C-shell.

[also Larry]
>Later that same day:
>
>Oops, it is possible in the Bourne shell.  What you can do
>is define a function (not well known in the Bourne shell)
>to do your cd's and set PS1.

Not all Bourne shells are bourne equal.  (Sorry)  *If* your Bourne
shell has shell functions, yes; if not, you are also correct.

How can you tell whether your Bourne shell has shell functions?
Try Larry's example chdir().  If sh says

	syntax error: `(' unexpected

it does not have shell functions.
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7690)
UUCP:	seismo!mimsy!chris	ARPA/CSNet:	chris@mimsy.umd.edu

eric@hpfcda.HP.COM (Eric Flink) (01/21/87)

I like having my current directory name appear in my prompt.
The following aliases appear in my .cshrc file:

    set prompt="(\! $cwd:t) % "
    alias cd 'set cdmark=$cwd; chdir \!* ; set prompt="(\! $cwd:t) % "'
    alias pushd 'pushd \!* ; set prompt="(\! $cwd:t) % "'
    alias popd 'popd; set prompt="(\! $cwd:t) % "'

Note: the :t modifier appended to $cwd strips all but the last
element of the path name.  (Otherwise your prompt might get very
long!)  I didn't invent these aliases, someone posted the idea
to USENET some time back.


Using the Korn shell, you can do the same thing with the following:

    PS1="(! \${PWD##*/}) $ "
    export PS1