Thursday, May 16, 2024
 Popular · Latest · Hot · Upcoming
14
rated 0 times [  14] [ 0]  / answers: 1 / hits: 8346  / 1 Year ago, fri, december 9, 2022, 6:05:21

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

More From » bash

 Answers
3

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


[#28807] Saturday, December 10, 2022, 1 Year  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
nnaisdio

Total Points: 369
Total Questions: 112
Total Answers: 108

Location: Kenya
Member since Mon, Jun 14, 2021
3 Years ago
;