6

I'm experiencing this weird issue where my exit status always return 0 even when it didn't execute successfully.

I want to output the exit status on my prompt 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

clearly the second last command ad didn't return a 0 status code. However that's what I got from the prompt.

Does anyone know what might be going on here?

EDIT 6/20 11:57AM: The issue seems to be that $? is always 0 no matter what, except there was an error within the .bashrc file itself, which will cause it to return a value different from 0.

Tri Nguyen
  • 9,950
  • 8
  • 40
  • 72
  • $(status) runs the command in a subshell - a child process. Pass $? as a parameter: $(status $?) and then: echo $1 in your status function. – jim mcnamara Jun 20 '13 at 14:35
  • 3
    The code as written seems to work fine for me. My `$BASH_VERSION` is `4.2.20(1)-release`. – IMSoP Jun 20 '13 at 14:36
  • 3
    It works fine for me to, it's enough to do this `PS1="\$?>"` – Grzegorz Żur Jun 20 '13 at 14:37
  • 1
    Out of interest, my prompt definition reads (in part) `export PS1='$(RET=$?; if [ $RET == 0 ]; then echo "\[\033[1;30m\]$RET"; else echo "\[\033[0;31m\]$RET"; fi;) \[\033[1;32m\]\u@\h'`, which shows the last return status in red if it was non-zero, or grey for zero. (The capture into `$RET` avoids the other logic over-writing `$?` before we've echoed it.) – IMSoP Jun 20 '13 at 14:44
  • @Grzegorz yeah that is enough, I put it out in a function because I wanted to do more complicated things with it, i.e. adding colors. – Tri Nguyen Jun 20 '13 at 15:08
  • @IMSoP I just tried to do the same thing on my machine. I still run into the same problem as described on my post. Any thoughts? – Tri Nguyen Jun 20 '13 at 15:12
  • 1
    @IMSoP my `$BASH_VERSION` is `3.2.48(1)-release`. I'm using the latest OS X Mountain Lion. – Tri Nguyen Jun 20 '13 at 15:13
  • @jimmcnamara thank you. I tried that, didn't work, still same issue. – Tri Nguyen Jun 20 '13 at 15:16
  • 1
    Hm, it's not just a version thing - I get the same (successful) results on `3.2.33(1)-release`. These are both Gentoo Linux boxes, so it's possible something else is different. Another possibility is that you have another special variable set (`$PS2` or `$PROMPT_COMMAND` perhaps?) which is over-writing `$?` somehow. – IMSoP Jun 20 '13 at 15:16
  • @IMSoP I do have a `$PS2`set indeed. This is a reason why? Is there a way to fix it without removing the `$PS2`? – Tri Nguyen Jun 20 '13 at 15:23
  • @TriNguyen Does blanking it fix it? If so, post your PS2 and maybe we can come up with a way around it. See also the answers to http://stackoverflow.com/questions/7135824/creating-a-bash-command-prompt-with-a-red-after-failure-of-previous-command?rq=1 – IMSoP Jun 20 '13 at 15:26
  • @IMSoP I tried to blank it now but that didn't fix it. I tried following the answers in the link you posted, and that didn't work either... – Tri Nguyen Jun 20 '13 at 15:43
  • In your editing you wrote: _The issue seems to be that $? is always 0 no matter what_ - this is clearly not so, as we see from your example `echo $?` output `127`; it rather seems that PS1 does not contain `$?` - you could `echo $PS1` to check. – Armali Sep 13 '13 at 12:04
  • @TriNguyen I also saw the similar problem. I am using Mac OSX 10.11. When I do the same for linux it works. I guess the problem is inside the .profile or .bash_rc file – solti Nov 20 '15 at 23:51

3 Answers3

1

Does the following work for you with your bash version?

export PS1="\$?>"
ikaerom
  • 538
  • 5
  • 27
0

I use the following in my $PS1:

 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

sanmiguel
  • 4,580
  • 1
  • 30
  • 27
0

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

OLD:

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

Always returned Zero no matter what.

NEW:

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

Now returned proper return value.

export PS1="\$(status)>"
solti
  • 4,339
  • 3
  • 31
  • 51