Bash exit status is always 0

I experience this strange problem when my completion status always returns 0, even when it did not succeed.

I want to display the completion status in my invitation with the following code:

function status() { echo $? } export PS1="\$(status)>" 

When I run this, I get the following output

  0❯ pwd /Users/tringuyen 0❯ ad bash: ad: command not found 0❯ echo $? 127 

it is clear that the second last ad command did not return the status code 0. However, this is what I got from the prompt.

Does anyone know what can happen here?

EDIT 6/20 11:57 AM: The problem is that $? always 0 regardless of the fact that an error occurred in the .bashrc file itself that will force it to return a value other than 0 .

+6
source share
3 answers

Does the next version work with your version of bash?

 export PS1="\$?>" 
+1
source

I also had a similar problem, but my function looked different. The problem was that I was missing a semicolon ";" after var = $?

OLD:

 function status() { VAR=$? echo $VAR } 

Zero is always returned no matter what.

NEW:

 function status() { VAR=$?; echo VAR; } 

The correct return value is now returned.

 export PS1="\$(status)>" 
+1
source

In my $ PS1, I use the following:

  PS1="\`if [ \$? = 0 ]; then echo \[\e[33m\]^_^\[\e[0m\]; else echo \[\e[31m\]\$? O_O\[\e[0m\]; fi\`" 

Src: https://github.com/sanmiguel/dotfiles/blob/master/bash/bash_functions.symlink#L63

0
source

Source: https://habr.com/ru/post/947745/


All Articles