Every command run in bash returns with an exit code.
Whenever I type a command on bash prompt, I want it to show the exit status, i.e., echo $?
e.g., if I run echo "hello";
on bash prompt, the output should be:
linux@linux$ hello
linux@linux$ 0
Every command run in bash returns with an exit code.
Whenever I type a command on bash prompt, I want it to show the exit status, i.e., echo $?
e.g., if I run echo "hello";
on bash prompt, the output should be:
linux@linux$ hello
linux@linux$ 0
The exit code from the last executed command is stored in the $?
environment variable. So you just can add this variable to the default command prompt and you will always have the exit code printed there. The prompt is stored in the $PS1
environment variable. It is initially set in the /etc/bash.bashrc
script and later in the $HOME/.bashrc
.
So edit the line in $HOME/.bashrc
(/etc/bash.bashrc
would be system wide) from it's default value:
PS1='${debian_chroot:+($debian_chroot)}u@h:w$ '
to this (for example):
PS1='${debian_chroot:+($debian_chroot)}u@h:w[$?] $ '
So the default prompt in changed to:
user@host:/path/to/dir[0] $
The 0 in the brackets is your exit code, see:
user@host:~[0] $ ls
user@host:~[0] $ ls /root/
ls: cannot open directory /root/: Permission denied
user@host:~[2] $ ^C
user@host:~[130] $
For the meanings see http://www.tldp.org/LDP/abs/html/exitcodes.html