Changing PS1

I have coded PS1 as shown, producing the following result when writing on the command line

 cdl[/media/ios120/chrisd/tomso-12.04/source]
 make tracepdf2d

If I make an error in the command an is printed

  cdl[/media/ios120/chrisd/tomso-12.04/source]
 ls-a
ls-a: command not found

My problem is that if I just press enter, I do not want to have the printed.

    PS1="\[\033[0;37m\]\342\224\214\342\224\200\$([[ \$? != 0 ]] && echo \" \[\033[0;31m\]\342\234\2
27\[\033[0;37m\]\342\224\")$(if [[ ${EUID} == 0 ]]; then echo '\[\033[0;31m\]\h'; else echo '\[\033[
01;33m\] cdl'; fi)\[\033[0;37m\][\[\033[01;34m\]\w\[\033[0;37m\]]\n\[\033[0;37m\]\342\224\224\342\22
4\200\342\224\200\342\225\274 \[\033[0m\]"


Insert a 'true' after it to force $? back to 0.

Unfortunately this will change the value of $? if you were wanting to depend on it later...

Not sure how to use 'true'

Command substitution never affects the value of $?, and that's the only way to execute anything from within the prompt string.

I don't think it's possible to cause the expansion of PS1 to alter the value of $?. If you (or anyone else) knows of a way, I'd appreciate knowing about it.

Since you neglected to be specific, I'm assuming this is for bash.

It seems like what you need is a way to detect when no command was entered at the prompt. Bash makes available the command number through the escape sequence \#. This value (at least with the version with which I tested) is not incremented for a null command. Testing its current value against its value at the time of the previous prompt's expansion should be sufficient to make a determination.

Since any variables set in the command substition subshell are lost when the shell exists, and since environmental changes cannot propagate from a child to a parent, the only way to store the command number for future inspection is to write it to a file.

The following example is intended only as a proof of concept. Aside from the minor inefficiency of reading a file for each prompt, it doesn't support simultaneously interactive shells (although it could, with some work and help from $$). The prompt consists simply of the exit status followed by a colon and a space. If no command was entered, regardless of the value of $?, a 0 is printed (analgous to not printing the X in your original problem statement):

PS1='$(ES=$?; LC=$(< ~/.lastcmd); if [ \# -eq ${LC:-0} ]; then echo 0; else echo $ES; fi; echo \# > ~/.lastcmd): '

Regards,
Alister