I'd like to incorporate the trick where I show the error code of the last command in my shell prompt. I've tried it - however it always only returns zero no matter what I ran.
Say I set the prompt to this:
export PS1="$? $ "
I get this output when I try typing correct and incorrect input:
0 $
0 $ echo foo
foo
0 $ echo $?
0
0 $ echofoo
bash: echofoo: command not found
0 $ echo $?
127
0 $
So the error code is captured, but not displayed on the shell prompt. I would expect that when I ran echofoo
, the next command line would show 127 $
, but it shows 0.
This happens too when I try running code which exits non-zero, e.g. this bad.py
Python file:
import sys
sys.exit(1)
0 $ python bad.py
0 $ echo $?
1
0 $
Again, I can see that it exits non-zero, but the shell prompt doesn't reflect that.
How can I correctly display the error code of the previous command in the shell prompt? I would like to know the answer for both bash and tcsh, as I haven't managed to get this working in either shell.