Subject: ksh, PS1 and $?
To: None <netbsd-users@netbsd.org>
From: Denis Lagno <dlagno@rambler.ru>
List: netbsd-users
Date: 04/28/2005 01:00:05
Hi,

I want to set "dynamic" prompt -- including output of some commands.
For example:

export PS1='[$(id -nu)@$(hostname -s) ($(tty | sed s/\\/dev\\///)) $(date "+%H:%M:%S") $(pwd) $?$ '

It works, but problem is that $? loses its value.  In particular, value in the end of the
prompt, just before $ is always 0.

I tried to fix it by using temporary file:

export RETVAL_BEFORE_PROMPT=`mktemp /tmp/retval_before_prompt.XXXXXXXX`
export PS1='[$(echo $? > $RETVAL_BEFORE_PROMPT; id -nu)@$(hostname -s) ($(tty | sed s/\\/dev\\///)) $(date "+%H:%M:%S") $(pwd) $(cat $RETVAL_BEFORE_PROMPT; sh -c "exit $(cat $RETVAL_BEFORE_PROMPT)")$ '

It is better.  Value at the end of the prompt before $ is really return value of previous command.
But if I try to echo $? it is still always 0.

Example:

[dina@chup (ttys6) 00:49:56 /sekuritat/home_dina 0$ true
[dina@chup (ttys6) 00:54:37 /sekuritat/home_dina 0$ false
[dina@chup (ttys6) 00:54:38 /sekuritat/home_dina 1$ echo $?
0
[dina@chup (ttys6) 00:54:43 /sekuritat/home_dina 0$

Any ideas how to make it work fine?